I have a lightning-button-menu in my lwc whos options are dynamic based on each record displayed, based on an change data capture its possible that the options will change when the user is viewing (external integration) so i have added the function for defining the menu options in the renderedCallback() however this seems to cause an infinite loop in SF.
As far as i can tell i am not updating an api value so it shouldn't be causing any kind of infinite looping.
Anyone got any ideas ?
Example HTML:
<lightning-icon size="small" title="Valid" alternative-text="Valid" variant="container" icon-name="utility:success" class="slds-m-right_small"></lightning-icon>
<lightning-formatted-text class="slds-text-link_faux" onclick={toggleContent} value={heading}> </lightning-formatted-text>
</div>
<lightning-button-menu slot="actions" size="medium" onselect={handleOnselect} alternative-text="Show menu"
menu-alignment="right">
<template for:each={menuOptions} for:item="option">
<lightning-menu-item label={option.label} value={option.value} key={option.value}>
</lightning-menu-item>
</template>
</lightning-button-menu>
</lightning-card>
</div>
</template>
Example JS:
@api line;
@track menuOptions =[];
renderedCallback(){
this.optionsList();
}
connectedCallback() {
this.optionsList();
}
optionsList(){
this.menuOptions = [];
let editobject = {label :edit, value : 'editObject'}
this.menuOptions.push(editobject);
//check if valid for view results and document
if(null !== this.line.Object_Valid__c){
if(this.line.Object_Valid__c){
console.log('line was seen as valid.');
let doc = {label :viewDoc,
value : 'viewDoc'}
this.menuOptions.push(doc);
let res = {label :viewResults,
value : 'viewResults'};
this.menuOptions.push(res);
}else{
console.log('line was NOT seen as valid.');
}
}
let copyMoveObject = {label :copyMoveLabel,
value : 'copyMove'}
this.menuOptions.push(copyMoveObject);
let removeObject = {label :remove,
value : 'remove'}
this.menuOptions.push(removeObject);
}
this.optionsList();
both in the connectedCallback and the renderedCallback? Why does connectedCallback not suffice? If you want to make the LWC sensitive to Change Data Capture, have a look at the empApi. renderedCallbacks take usually measures not to be executed more than once - your renderedCallback looks sure to trigger a rerendering due to changingmenuOptions
.