What is refreshApex and how do you use it in an LWC?
Answer
refreshApex (from @salesforce/apex) forces a wired Apex method to re-fetch data and update the cache, so the UI shows fresh results after a change.
You must keep a reference to the entire wired response, not just the data:
javascript
import { refreshApex } from '@salesforce/apex';
@wire(getContacts, { accountId: '$recordId' })
wiredContacts(result) {
this.wiredResult = result;
if (result.data) this.contacts = result.data;
}
async handleSave() {
await saveContact({ c: this.contact });
await refreshApex(this.wiredResult);
}
For Lightning Data Service records use notifyRecordUpdateAvailable instead.
lwcrefreshapexwire