Sub topic 3
How @wire provisions data
This chapter assumes you can write a basic Lightning Web Component (HTML template, JavaScript class, @api properties). It also assumes you know how to read a simple Apex method. You have already seen how to expose a method with @AuraEnabled(cacheable=true) and import it into a component. Here you will learn what @wire does with that import.
The working model
@wire is a decorator that connects a component to a data source. The data source is called a wire adapter. When you import an Apex method from @salesforce/apex, that import is a wire adapter, and the framework can call it for you.
You do not call a wired method yourself. You describe what you want, and the framework does the following:
- It reads the parameters you passed to
@wire. - It calls the Apex method when the component is connected to the page and the parameters are ready.
- It checks the client-side cache before it contacts the server.
- It hands the result to your property or function as an object with
dataanderror. - It repeats the call when a reactive parameter changes.
A minimal wire
import { LightningElement, api, wire } from 'lwc';
import getContacts from '@salesforce/apex/ContactController.getContacts';
export default class ContactList extends LightningElement {
@api recordId;
@wire(getContacts, { accountId: '$recordId' })
contacts;
}The decorator takes two arguments. The first is the adapter, here the imported getContacts. The second is a configuration object. Its keys must match the Apex parameter names exactly, so accountId matches getContacts(Id accountId).
The property contacts is not something you assign. The framework writes to it. Its value starts as undefined, then becomes an object with a data key or an error key.
What the framework provisions
| Moment | `contacts.data` | `contacts.error` |
|---|---|---|
| Before the first response | `undefined` | `undefined` |
| Call succeeded | The returned records | `undefined` |
| Call failed | `undefined` | The error object |
Each new response replaces the whole object. Code that reads contacts.data always sees the latest result, and the template re-renders when it changes.
Timing and the undefined parameter
The framework waits until the parameters are defined. If recordId is still undefined when the component connects, the wire does not call Apex. This is deliberate. Your component often receives recordId slightly after it is created, and the wire runs once the value arrives.
This means a wired property can stay empty with no error. If nothing appears, first check that every reactive parameter has a value.
Provisioned data is read-only
The data value is frozen. Writing to it, for example this.contacts.data.push(...) or setting a field on a record, throws an error in strict mode or fails without notice. If you need to change the records, copy them first.
const copy = this.contacts.data.map((row) => ({ ...row }));Common errors
- Calling a non-cacheable method. A method without
cacheable=truecannot be wired. The deployment succeeds, but the wire fails when the component loads. - Mismatched parameter names. A key such as
accountinstead ofaccountIdmeans Apex receivesnull. - Missing
$on a reactive value.{ accountId: 'recordId' }passes the literal textrecordId. The next chapter covers the$prefix. - Mutating
data. The provisioned object is read-only. Copy it before sorting or editing. - Assuming the call runs at a fixed time. The framework decides when to call. Do not place logic in the component that depends on the call having already happened.
Check your understanding
Exercise
A component declares @wire(getContacts, { accountId: '$recordId' }) contacts;. The page loads, but contacts.data and contacts.error are both undefined, and no request reaches Apex. Give one likely cause and how you would confirm it.
Show answer
recordId is probably still undefined, so the wire is waiting for a defined parameter. Confirm it by logging this.recordId in connectedCallback or in a getter, and check that the component is placed where a record ID is supplied, such as a record page. Another cause is that the Apex method is not marked cacheable=true, which you can check in the Apex class.