Sub topic 10
Refreshing when wire parameters are reactive
This chapter assumes you know how to wire a cacheable Apex method, how @AuraEnabled methods work, and how async/await behaves. It also assumes you have seen refreshApex used with a property wire and a function wire.
What reactive parameters change
A wire parameter prefixed with $ is reactive. When the referenced component property changes, the wire service calls the Apex method again with the new value. It does this on its own, so you never call refreshApex for that case.
refreshApex solves a different problem. The parameters have not changed, but the server data has. In that case the wire service has no reason to re-run, so it keeps serving the cached result.
The two mechanisms are independent:
| Situation | What triggers a new fetch |
|---|---|
| `$recordId` changes to a new value | The wire service, automatically |
| Same `$recordId`, but a contact was edited | You, by calling `refreshApex` |
| `$recordId` changes while `refreshApex` is running | The wire service re-provisions for the new value |
Refresh uses the current parameters
The wired result object remembers which wire it came from. When you call refreshApex(this.contacts), the wire service re-fetches with the parameter values the wire currently has. You do not pass the parameters again.
This example adds a filter. The wire is reactive on accountId. The user picks an account from a list, and saving a contact refreshes whichever account is selected.
import { LightningElement, wire } from 'lwc';
import { refreshApex } from '@salesforce/apex';
import getContacts from '@salesforce/apex/ContactController.getContacts';
import updateEmail from '@salesforce/apex/ContactController.updateEmail';
export default class AccountContacts extends LightningElement {
selectedAccountId;
@wire(getContacts, { accountId: '$selectedAccountId' })
contacts;
handleAccountChange(event) {
// The wire re-runs by itself. No refreshApex is needed here.
this.selectedAccountId = event.detail.accountId;
}
async handleSave(event) {
const { contactId, email } = event.detail;
await updateEmail({ contactId, email });
// Re-fetches for the account that is selected right now.
await refreshApex(this.contacts);
}
}What happens before a parameter is set
While selectedAccountId is undefined, the wire does not call Apex. The property contacts holds no data and no error. If the user saves before choosing an account, this.contacts still exists, but there is nothing useful to refresh.
Guard against that case when a save can happen before the wire has run:
async handleSave(event) {
const { contactId, email } = event.detail;
await updateEmail({ contactId, email });
if (this.selectedAccountId) {
await refreshApex(this.contacts);
}
}Two accounts, two cache entries
The client cache stores one entry per combination of method and parameter values. Suppose the user views account A, switches to account B, and then goes back to A. The wire may serve A from the cache, which can be stale if a contact for A changed in the meantime.
refreshApex refreshes only the entry for the current parameters. It does not clear the entry for account B. If B is shown again later, it may still be stale. Treat this as a known limit of the cache and plan for it. For example, call refreshApex when the user returns to an account whose data may have changed elsewhere.
Diagram
Common mistakes in this area
- Calling
refreshApexafter you change a reactive parameter. The wire already re-runs, so the call adds nothing. - Passing the parameters to
refreshApex. It takes only the wired result. - Expecting a refresh of one account to update the cached data for other accounts.
Exercise
A component has @wire(getContacts, { accountId: '$accountId' }) contacts;. The user selects a new account, and the list updates without any extra code. Later the user edits a contact on the same account, and the list stays the same. Why does the first case need no refreshApex, and what do you call in the second case?
Show answer
In the first case the reactive parameter changed, so the wire service calls Apex again on its own. In the second case the parameter is the same and the cache still holds the old result, so after the save you call await refreshApex(this.contacts).