Sub topic 10
Refreshing cached results with refreshApex
This chapter assumes you can write a cacheable @AuraEnabled method, wire it to a property in a Lightning web component, and call a non-cacheable method imperatively. Here you will learn why a wired result goes stale after a change, and how refreshApex fixes that.
Why wired data goes stale
A method marked cacheable=true has its results stored on the client. When a component wires that method again with the same parameters, Lightning can serve the stored result without contacting the server. This makes reads fast.
The cost is that the cache does not know about your writes. Suppose a component lists contacts through @wire, and another method inserts a new contact. The insert succeeds on the server, but the wired list still shows the old data, because the cached result is still considered valid.
refreshApex tells the platform to discard the cached result for a wired property and request fresh data from the server.
The basic pattern
Import refreshApex from @salesforce/apex. Pass it the wired property itself, then await the returned promise.
import { LightningElement, api, wire } from 'lwc';
import { refreshApex } from '@salesforce/apex';
import getContacts from '@salesforce/apex/ContactController.getContacts';
export default class ContactListRefreshable extends LightningElement {
@api recordId;
@wire(getContacts, { accountId: '$recordId' })
contacts;
async handleRefresh() {
await refreshApex(this.contacts);
}
}When the refresh completes, the wire service assigns a new value to this.contacts. Any template expression that reads contacts.data re-renders.
Refreshing after a write
The most common use is to refresh right after an imperative call changes data. Combine the non-cacheable createContact method with the wired list:
import { LightningElement, api, wire } from 'lwc';
import { refreshApex } from '@salesforce/apex';
import getContacts from '@salesforce/apex/ContactController.getContacts';
import createContact from '@salesforce/apex/ContactController.createContact';
export default class ContactManager extends LightningElement {
@api recordId;
lastName = '';
error;
@wire(getContacts, { accountId: '$recordId' })
contacts;
handleChange(event) {
this.lastName = event.target.value;
}
async handleSave() {
try {
await createContact({
lastName: this.lastName,
accountId: this.recordId
});
this.error = undefined;
await refreshApex(this.contacts);
} catch (error) {
this.error = error.body ? error.body.message : error.message;
}
}
}The order matters. The refresh runs only after the insert has finished, so the new record is included in the fresh query. If createContact throws, control jumps to catch and no refresh happens.
The flow looks like this:
Common errors
- Passing the wrong value.
refreshApex(this.contacts.data)does nothing useful. Pass the whole wired property, the same object the wire service gave you. - Forgetting to await.
refreshApexreturns a promise. Withoutawait, code that follows it may run before the new data arrives. - Refreshing a non-wired call. If you called a method imperatively and stored the result yourself, there is nothing to refresh. Call the method again and assign the result.
- Calling it before the write finishes. Refreshing in parallel with the write can return the old rows.
When you wire to a function instead of a property, keep the whole result in a field. Then pass that field to refreshApex:
@wire(getContacts, { accountId: '$recordId' })
wiredContacts(result) {
this.wiredResult = result;
this.contacts = result.data;
}Here you would call refreshApex(this.wiredResult). Passing this.contacts would fail, because it holds only the data.
Exercise
A component wires getContacts to a property named contacts. After an imperative save succeeds, the list still shows old data, and the code calls refreshApex(this.contacts.data). What is wrong, and what is the fix?
Show answer
refreshApex needs the whole wired value, not only its data field. Call await refreshApex(this.contacts) after the save promise resolves.