Sub topic 11
Why a refresh does nothing: common mistakes
This chapter assumes you can wire a cacheable Apex method, write an @AuraEnabled method, and use async/await. It covers the reasons refreshApex can seem to do nothing, and how to find each one.
refreshApex fails quietly. It returns a promise, and it doesn't throw if you give it the wrong value. The UI just keeps showing old data. Most failures come down to a few causes.
How a refresh works
refreshApex takes the object that the wire gave you. That object is the link back to the wire, so the platform knows which Apex call to run again. If you pass anything else, there's nothing to refresh.
Mistake 1: passing part of the result
A wire result has the shape { data, error }. Passing data or a rebuilt copy hands over a plain value that has no link to the wire.
// Wrong: refreshApex needs the object the wire provisioned, not a part of it.
await refreshApex(this.contacts.data);
// Wrong: copying the values into your own object loses the link to the wire.
await refreshApex({ data: this.contacts.data, error: this.contacts.error });
// Right: pass the original wired result.
await refreshApex(this.contacts);Mistake 2: not storing the result of a function wire
With a property wire, the property holds the result. With a function wire, the platform passes the result to your function, and it's gone after the function returns unless you save it.
@wire(getContacts, { accountId: '$recordId' })
wiredContacts(result) {
this.wiredContactsResult = result; // keep the whole object
const { data, error } = result;
// ...
}Store result first, before you destructure it. Then call refreshApex(this.wiredContactsResult).
Mistake 3: not waiting for the save
The refresh must run after the save completes. If you skip await, the refresh may fetch the old record, and the UI looks unchanged.
async handleSave(event) {
const { contactId, email } = event.detail;
await updateEmail({ contactId, email }); // wait for the DML
await refreshApex(this.contacts); // then refresh
}Mistake 4: refreshing the wrong wire
refreshApex refreshes the wire whose result you pass in. If two components wire the same method, or one component wires two methods, refreshing one doesn't update the others. Pass the result for each wire that shows changed data.
Mistake 5: trying to save with a cacheable method
Cacheable methods can't modify data. If you put DML in a method marked cacheable=true, the platform rejects it. Keep the read method cacheable and put the save in a separate @AuraEnabled method without cacheable=true.
Mistake 6: swallowing errors
If the save fails and you don't catch the rejection, the refresh never runs. Use try/catch around both calls, as in the earlier chapter on error handling, so a failure shows up instead of looking like a stale screen.
A checklist for debugging
- Log the value you pass to
refreshApex. It should havedataanderrorkeys, and it should be the same object the wire provided. - Confirm the save promise resolves before the refresh starts.
- Check the record in the org to see whether the save actually changed it.
- Check that the refreshed wire is the one the template reads from.
Exercise
A function wire handler reads const { data, error } = result; and sets this.contacts = data;. After a save, the code calls await refreshApex(this.contacts); and the list does not update. Give two reasons this fails and the fix.
Show answer
First, this.contacts holds only data, not the wire's result object, so refreshApex has nothing to refresh. Second, nothing stored the full result object. The fix is to add this.wiredContactsResult = result; in the wire function and call await refreshApex(this.wiredContactsResult);.