Sub topic 6
Refreshing a function wire by keeping the result
This chapter assumes you can write a basic LWC, wire a cacheable Apex method, and use async/await. You should also know what the wired result object (data and error) looks like. Here you will learn how to refresh a wire that is bound to a function.
Why function wires need extra work
When you bind a wire to a property, LWC stores the whole result object in that property. You can pass the property to refreshApex directly.
When you bind a wire to a function, LWC calls your function with the result object. LWC does not store the object for you. If you only read data and error and throw the rest away, you have nothing to give refreshApex later.
The fix is to save the object the function receives. refreshApex uses it to find the wire it should refresh.
Keep the result
Add a field that holds the whole argument. Assign it first in the function, before you read data or error.
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 ContactList extends LightningElement {
@api recordId;
contacts;
error;
wiredContactsResult;
@wire(getContacts, { accountId: '$recordId' })
wiredContacts(result) {
this.wiredContactsResult = result;
const { data, error } = result;
if (data) {
this.contacts = data;
this.error = undefined;
} else if (error) {
this.error = error;
this.contacts = undefined;
}
}
async handleSave(event) {
const { contactId, email } = event.detail;
try {
await updateEmail({ contactId, email });
await refreshApex(this.wiredContactsResult);
} catch (e) {
this.error = e;
}
}
}Three things matter here:
wiredContactsResultholds the full result object. The function stores it on every call, so it always points to the latest one.contactsanderrorare your own fields for the template. You can name and shape them as you like.handleSavepassesthis.wiredContactsResulttorefreshApex, notthis.contacts.
The Apex side
The read method is cacheable, so a wire can use it. The update method is not cacheable, because cacheable methods cannot modify data.
public with sharing class ContactController {
@AuraEnabled(cacheable=true)
public static List<Contact> getContacts(Id accountId) {
return [
SELECT Id, Name, Email
FROM Contact
WHERE AccountId = :accountId
WITH USER_MODE
ORDER BY Name
LIMIT 50
];
}
@AuraEnabled
public static void updateEmail(Id contactId, String email) {
Contact c = new Contact(Id = contactId, Email = email);
update as user c;
}
}What happens when you refresh
handleSave awaits the DML call first. If it succeeds, refreshApex asks the wire to fetch the data again from the server. The wire then calls your function with a new result object. Your function stores it and updates contacts, and the template re-renders.
The order of the two awaits matters. If you refresh before the save finishes, you re-fetch the old data.
Common mistakes with function wires
| Mistake | Result |
|---|---|
| Never storing `result` | You have nothing to pass, so you cannot refresh. |
| Passing `this.contacts` (the `data` array) | `refreshApex` cannot find the wire, so nothing happens. |
| Storing `{ data, error }` as a new object | The copy is not linked to the wire. |
| Storing `result` only inside `if (data)` | After an error, your stored result is stale or missing. |
Store the result on every call, before any branching, and the last row cannot occur.
Exercise
A function wire stores only this.contacts = data. After updateEmail succeeds, refreshApex(this.contacts) has no effect. What is wrong, and how do you fix it?
Show answer
this.contacts is only the data array, not the result object the wire provisioned, so refreshApex has no wire to refresh. Add a field such as wiredContactsResult, assign the full result argument to it at the top of the wired function, and call refreshApex(this.wiredContactsResult).