Sub topic 3
What a wire gives you: the result object
This chapter assumes you can write a basic LWC with @wire calling a cacheable Apex method, and that you are comfortable with destructuring and async/await. Before you can refresh a wire, you need to know exactly what the wire hands you. refreshApex works on that object, not on your data.
The shape of the result
A wire adapter provisions a single object with two properties:
data: the records or values returned by Apex. It isundefineduntil a response arrives, and also after an error.error: the error information if the call failed. It isundefinedwhen the call succeeds.
Only one of the two is set at a time. Before the first response, both are undefined.
Two ways to receive it
You can bind a wire to a property or to a function. Both receive the same kind of object.
With a property wire, the property holds the whole result object:
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 not the list of contacts. It is { data, error }. The list is this.contacts.data.
With a function wire, the runtime calls your function each time a new result is provisioned and passes the object as the argument:
@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;
}
}Here you decide what to store. The line this.wiredContactsResult = result keeps the original object. The data and error values you copy into other fields are only parts of it.
Reading it in a template
For a property wire, the template reads the parts directly from the property:
<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>When a new result arrives, the property is reassigned and the template re-renders.
Why the object matters
The result object is not a plain copy of the response. The wire service tracks it and links it to the wire that produced it. refreshApex uses that link to find which wire to re-run. If you pass something else, such as data alone or a new object you built, the link is gone.
You can see the difference in code:
// Wrong: this is only the data, so there is no link to the wire.
await refreshApex(this.contacts.data);
// Wrong: a copy has the same fields but is not the provisioned object.
await refreshApex({ data: this.contacts.data, error: this.contacts.error });
// Right: the original object the wire provisioned.
await refreshApex(this.contacts);The next chapter covers refreshApex itself. For now, remember that the object has to stay intact.
Data is read-only
The data value provisioned by a wire is read-only. Assigning to a field on a record, such as this.contacts.data[0].Email = 'x', throws an error. If you need to edit values locally, make a copy first, for example with structuredClone or by mapping to new objects. Do not try to mutate the wired value.
Summary of what to keep
| Wire style | What you hold | What to keep for a refresh |
|---|---|---|
| Property wire | The property is the result object | The property itself |
| Function wire | The function parameter is the result object | A field that stores the whole parameter |
Exercise
A function wire handler sets this.contacts = result.data. A teammate then calls refreshApex(this.contacts) after saving and nothing updates. What is wrong, and how do you fix it?
Show answer
this.contacts holds only the data part, not the result object the wire provisioned, so refreshApex has no link to the wire. Store the whole result parameter in a separate field, such as this.wiredContactsResult = result, and pass that field to refreshApex.