Sub topic 4
Using refreshApex
What you need first
This chapter assumes you can write a basic Lightning Web Component and wire a cacheable Apex method to it. It also assumes you know @AuraEnabled methods, SOQL, and async/await. The earlier chapters in this tutorial cover why cached wire data can go stale and what the wired result object contains. This chapter covers the function that refreshes it.
What refreshApex does
refreshApex is a function exported by @salesforce/apex. You give it a wired result, and it:
- Skips the client-side cache and calls the Apex method on the server again.
- Updates the cache with the response.
- Provisions the new value to your wire, so the component re-renders.
It returns a Promise that resolves when the refresh finishes. You can await it to run code after the new data has arrived.
import { refreshApex } from '@salesforce/apex';
await refreshApex(this.contacts);The import is a named import from @salesforce/apex. Don't confuse it with @salesforce/apex/ContactController.getContacts, which is the path for importing an Apex method.
What to pass in
refreshApex needs the whole object the wire service gave your component. That object has data and error properties. The wire service uses it to find which wire, with which parameters, to re-run. A copy or a piece of it has no connection to the wire, and the call silently does nothing.
How you get that object depends on how you declared the wire:
| Wire declared on | What you pass to refreshApex |
|---|---|
| A property | The property itself |
| A function | The result object you saved from the function's argument |
The next two chapters work through both cases in detail. Here is the property form:
import { LightningElement, api, wire } from 'lwc';
import { refreshApex } from '@salesforce/apex';
import getContacts from '@salesforce/apex/ContactController.getContacts';
export default class ContactListSimple extends LightningElement {
@api recordId;
@wire(getContacts, { accountId: '$recordId' })
contacts;
handleRefresh() {
return refreshApex(this.contacts);
}
}The contacts property holds { data, error }, so you pass this.contacts directly. The template reads contacts.data and contacts.error.
How a refresh flows
When to call it
Call refreshApex when the data your wire shows has changed and the wire cannot know that. Typical triggers are:
- The user saved a change through a separate imperative Apex call.
- A child component fired an event that tells the parent something changed.
- The user clicked a "Refresh" button.
A later chapter covers the save-then-refresh pattern in full.
Await the result
Because the function returns a Promise, you can wait for it. This lets you sequence work, such as hiding a spinner after the new data has loaded:
async handleRefresh() {
this.isLoading = true;
try {
await refreshApex(this.contacts);
} finally {
this.isLoading = false;
}
}If the server call fails, the Promise can reject, so the try block matters. A later chapter covers error handling in depth.
Exercise
A component has @wire(getContacts, { accountId: '$recordId' }) contacts;. A teammate writes await refreshApex(this.contacts.data); and reports that the list never updates. What is wrong, and what is the fix?
Show answer
refreshApex needs the whole result object the wire provisioned, not its data property. A part of the result has no link to the wire, so the call does nothing. The fix is await refreshApex(this.contacts);.