Sub topic 12
When to use something other than refreshApex
This chapter assumes you can wire a cacheable Apex method, call a non-cacheable method imperatively, and use refreshApex after a save. It covers the cases where refreshApex is the wrong tool and what to use in its place.
What refreshApex is good at
refreshApex re-runs one wired Apex method and updates the wire with the new result. It fits when your component owns the Apex call and knows when the data changed. It has three limits:
- It only refreshes the wire result you pass in. Other components that wire the same method are not updated.
- It only runs when your code calls it. It does not notice changes made by other users or other components.
- It costs a server round trip, even when only one field changed.
Choosing an approach
| Situation | Better choice |
|---|---|
| You read and edit fields on a single record | Lightning Data Service (LDS) with `getRecord` and `updateRecord` |
| Data must never come from the cache | Imperative call to a non-cacheable method |
| A change made elsewhere must reach your component | Platform events with `lightning/empApi`, or a parent that triggers the refresh |
| Several wires depend on one save | `refreshApex` on each wire, run together |
Use Lightning Data Service for single records
If you only need fields from one record, Apex is extra work. LDS caches records, shares them across components, and updates every component that uses the record after updateRecord succeeds. You write no refresh code.
import { LightningElement, api, wire } from 'lwc';
import { getRecord, updateRecord, getFieldValue } from 'lightning/uiRecordApi';
import EMAIL_FIELD from '@salesforce/schema/Contact.Email';
import ID_FIELD from '@salesforce/schema/Contact.Id';
export default class ContactEmail extends LightningElement {
@api recordId;
@wire(getRecord, { recordId: '$recordId', fields: [EMAIL_FIELD] })
contact;
get email() {
return getFieldValue(this.contact.data, EMAIL_FIELD);
}
async handleSave(event) {
const fields = { [ID_FIELD.fieldApiName]: this.recordId, [EMAIL_FIELD.fieldApiName]: event.detail.email };
await updateRecord({ fields });
// No refreshApex needed. LDS updates the wire.
}
}Use Apex when you need a filtered list, an aggregate, a join across objects, or logic that LDS cannot express.
Skip the cache when freshness matters
A wired cacheable method can serve a cached value. If a screen must always show current data, such as a balance or an approval status, call a non-cacheable method imperatively. This is also simpler when the data loads once in connectedCallback and you never need reactive updates.
import { LightningElement, api } from 'lwc';
import getLiveContacts from '@salesforce/apex/ContactController.getLiveContacts';
export default class LiveContacts extends LightningElement {
@api recordId;
contacts;
error;
async connectedCallback() {
try {
this.contacts = await getLiveContacts({ accountId: this.recordId });
} catch (e) {
this.error = e;
}
}
}The cost is that every load hits the server and you give up the reactive wire. Use this approach only where stale data is a real problem.
Changes you did not make
refreshApex cannot react to a change in another browser tab or by another user. Your options are:
- Subscribe to a platform event or Change Data Capture event with
lightning/empApi, then callrefreshApexin the handler. - Have a parent component dispatch or call a method on the child so that it refreshes.
- Poll on a timer. This is the last resort, because it adds load whether or not anything changed.
Keep refreshApex as the final step in each of these. The alternatives only decide when the refresh happens.
Refreshing more than one wire
When one save affects two wired results, refresh both and wait for both:
await Promise.all([
refreshApex(this.contacts),
refreshApex(this.accountSummary)
]);Exercise
A component shows a filtered list of open Cases from a cacheable Apex method. Another component, owned by a different team, edits those Cases. Which approach lets the list update without polling, and why is refreshApex alone not enough?
Show answer
Subscribe to a Change Data Capture or platform event with lightning/empApi and call refreshApex in the event handler. refreshApex alone is not enough because it runs only when your own code calls it, and your component has no signal that another component saved a Case. A filtered list also rules out plain LDS, since getRecord reads a single record.