Sub topic 5
Refreshing a property wire
This chapter assumes you can write a basic Lightning Web Component, wire a cacheable Apex method, and use async/await. It covers the simplest form of refresh: a wire bound to a property.
What a property wire holds
When you decorate a property with @wire, the platform assigns it an object with two fields, data and error. Before the first response arrives, both are undefined. After a successful response, data holds the result. After a failure, error holds the error.
import { LightningElement, api, wire } from 'lwc';
import getContacts from '@salesforce/apex/ContactController.getContacts';
export default class ContactListSimple extends LightningElement {
@api recordId;
@wire(getContacts, { accountId: '$recordId' })
contacts;
}Here this.contacts is the whole wired result. It is not the list of contacts. The list is this.contacts.data.
Passing the property to refreshApex
refreshApex is imported from @salesforce/apex. It takes the value the wire provisioned, asks the server for fresh data, and updates the wire with the response. With a property wire, the property already holds that value, so you pass the property itself.
import { LightningElement, api, 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 ContactListSimple extends LightningElement {
@api recordId;
@wire(getContacts, { accountId: '$recordId' })
contacts;
async handleSave(event) {
const { contactId, email } = event.detail;
await updateEmail({ contactId, email });
await refreshApex(this.contacts);
}
}The sequence in handleSave matters:
updateEmailruns a non-cacheable Apex method that performs the DML.- When that call succeeds,
refreshApex(this.contacts)bypasses the client cache and requests the data again. - The wire provisions a new value, and the template re-renders.
Reading the wire in the template
The template reads data and error from the same property. Nothing in it needs to change after a refresh. When the wire provisions a new value, the template re-renders.
<template>
<template lwc:if={contacts.data}>
<template for:each={contacts.data} for:item="contact">
<p key={contact.Id}>{contact.Name}: {contact.Email}</p>
</template>
</template>
<template lwc:if={contacts.error}>
<p>Could not load contacts.</p>
</template>
</template>Common mistakes with a property wire
Passing anything other than the original wired result is the usual reason a refresh has no effect. These calls look reasonable but fail:
// Wrong: refreshApex needs the object the wire provisioned, not a part of it.
await refreshApex(this.contacts.data);
// Wrong: copying the values into your own object loses the link to the wire.
await refreshApex({ data: this.contacts.data, error: this.contacts.error });
// Right: pass the original wired result.
await refreshApex(this.contacts);The wire tracks the result object it created. A copy or a nested field is a different object, so refreshApex cannot tell which wire to update.
When to prefer this form
Use the property form when you only need to display data and error and do not need to run code each time a value arrives. If you need to transform the data or keep your own copy, bind the wire to a function instead. That form is covered in the next chapter, and it requires you to store the result yourself.
Exercise
A component has @wire(getContacts, { accountId: '$recordId' }) contacts;. After a successful save, a developer calls await refreshApex(this.contacts.data); and the list does not update. What is wrong, and what is the fix?
Show answer
this.contacts.data is only the list of records, not the result object the wire provisioned, so refreshApex cannot link it to the wire. Pass the property itself: await refreshApex(this.contacts);.
Exercise
Why must the save be done with a non-cacheable Apex method and not the wired getContacts method?
Show answer
Cacheable methods cannot modify data. The DML must go in a separate method annotated with @AuraEnabled without cacheable=true, called imperatively. Then refreshApex re-reads the data through the cacheable method.