How we handle dependent wire in LWC?
For example :
@wire(getObjectInfo, { objectApiName: OPP_OBJECT })
objectInfo;
@wire(getPicklistValues, {
recordTypeId: '$objectInfo.data.defaultRecordTypeId',
fieldApiName: STATUS_FIELD
})
pickListValues;
@wire(getRecord, { recordId: '$recordId', fields: FIELDS })
wiredRecord({ error, data }) {
if (ldsErrorHandling(error) && data) {
this._status = getFieldValue(data, STATUS_FIELD);
}
}
I have an operation that depends on the Stage Field of opportunity and also the picklist values. With normal promises or callbacks I can know when the two values are not null. If is a normal apex call we can have both information at the same time. But how we handle this type of situations with @wire
.
My solution for this (is this the only way?):
@wire(getObjectInfo, { objectApiName: OPP_OBJECT })
objectInfo;
@wire(getPicklistValues, {
recordTypeId: '$objectInfo.data.defaultRecordTypeId',
fieldApiName: STATUS_FIELD
})
handlePickValues({ error, data }) {
if (ldsErrorHandling(error) && data) {
this._data = data;
this.dependentLogic();
}
}
@wire(getRecord, { recordId: '$recordId', fields: FIELDS })
wiredRecord({ error, data }) {
if (ldsErrorHandling(error) && data) {
this._status = getFieldValue(data, STATUS_FIELD);
this.dependentLogic();
}
}
dependentLogic() {
if (this._status && this._data) //do logic
}
Thank you in advance
getPicklistValues
andgetRecord
with functions instead putting directly in a var. Inside the function I call a method. On top of this method I verify if both var are not null, and then I do the logic. I don't like this approach, and I like to know exactly what my code is doing.if (this.objectInfo.data && this.picklistValue.data) { ... }
@wire
the only way I know is to call two or more times(depending how many dependencies) the same function and check if all values that we need is not null. And I was asking if has a better way to do.