Sub topic 9
Refreshing wired data with refreshApex
This chapter assumes you can write a wired Apex method, import it into a component, and render data and error. If not, review the chapters on wiring to a property and to a function first.
Why wired data goes stale
A method marked @AuraEnabled(cacheable=true) is cached on the client. When your component changes a record, for example by saving a new contact through a form or an imperative Apex call, the wire does not know. It keeps showing the cached result until its parameters change or the cache entry expires.
refreshApex tells the wire to fetch fresh data from the server and update the wired result.
What refreshApex needs
refreshApex takes the whole value that the wire provisioned. That value is the object with data and error, not just the data inside it. You must keep a reference to it.
This is the most common mistake. If you pass this.rows (the unpacked array), nothing refreshes, because the function cannot tell which wire to refresh.
Wiring to a property
If you wire to a property, the property already holds the whole value. Pass it directly:
import { refreshApex } from '@salesforce/apex';
handleRefresh() {
return refreshApex(this.contacts);
}Here contacts is the property from @wire(getContacts, { accountId: '$recordId' }) contacts;. In the template you read contacts.data and contacts.error as before.
Wiring to a function
If you wire to a function, the wire gives you the value as an argument. Store it in a field so you can pass it later:
import { LightningElement, api, wire } from 'lwc';
import { refreshApex } from '@salesforce/apex';
import getContacts from '@salesforce/apex/ContactController.getContacts';
export default class ContactRefresh extends LightningElement {
@api recordId;
wiredResult;
rows;
@wire(getContacts, { accountId: '$recordId' })
wiredContacts(result) {
this.wiredResult = result;
if (result.data) {
this.rows = result.data;
}
}
handleRefresh() {
return refreshApex(this.wiredResult);
}
}The function receives result and saves it in wiredResult. It also copies result.data into rows for the template. handleRefresh passes wiredResult, not rows, to refreshApex.
refreshApex returns a promise. Return it from your handler, or await it if you need to run code after the refresh finishes.
The refresh flow
Refresh after the change has finished. If you call refreshApex while an update is still in flight, the server may return the old data.
Common errors
| Symptom | Likely cause |
|---|---|
| The list does not update | You passed `data` instead of the whole wired value |
| `refreshApex` throws on an undefined value | The wire has not provisioned yet, so the stored field is empty |
| The list updates but shows old rows | The refresh ran before the save completed |
| The template shows nothing after refresh | In function mode, you only copy `data` when it is truthy, so the previous value remains until new data arrives |
A refresh also re-runs your wired function. If you set rows only when result.data exists, an error result leaves the old rows in place. Clear them in an else if (result.error) branch if you want errors to replace stale data.
Refreshing after an imperative call
A common pairing is an imperative call that changes data, followed by a refresh of the wire that displays it:
async handleSave() {
try {
await saveContact({ contact: this.draft });
await refreshApex(this.wiredResult);
} catch (error) {
this.errorMessage = error.body ? error.body.message : error.message;
}
}Awaiting the save first guarantees the refresh reads the new state.
Exercise
A component wires getContacts to a function and stores result.data in this.rows. A button handler calls refreshApex(this.rows), but the list never updates. What is wrong, and how do you fix it?
Show answer
refreshApex needs the whole value the wire provisioned, not the unpacked data. Store the result argument in a field such as this.wiredResult inside the wired function, and call refreshApex(this.wiredResult).
Exercise
Why should you await an imperative save before calling refreshApex?
Show answer
If the refresh starts before the save completes, the server can return the old data. Awaiting the save ensures the refresh reads the updated records.