Sub topic 2
Wire adapters and the @wire decorator
This chapter assumes you know JavaScript modules and async/await, the structure of a Lightning web component, and how @api recordId works on a record page. Here you learn what a wire adapter is and how the @wire decorator connects one to your component. The chapters that follow cover getRecord itself in detail.
What a wire adapter is
A wire adapter is a function-like module export that supplies data to a component. The wire service, which is part of the Lightning Web Components framework, runs the adapter for you. You never call it directly. You describe which adapter you want and which values to pass it. The framework then fetches the data, delivers it to your component, and delivers it again whenever the data changes.
The uiRecordApi module exports several adapters, including getRecord. It also exports plain functions such as updateRecord. Those are called imperatively, so @wire does not apply to them.
The syntax
@wire takes two arguments: the adapter you imported, and a configuration object. Place the decorator on a property or on a method.
import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Account.Name';
export default class WireForms extends LightningElement {
@api recordId;
// Form 1: wire to a property
@wire(getRecord, { recordId: '$recordId', fields: [NAME_FIELD] })
account;
// Form 2: wire to a function
@wire(getRecord, { recordId: '$recordId', fields: [NAME_FIELD] })
handleAccount({ data, error }) {
if (data) {
console.log('Loaded', data.id);
} else if (error) {
console.error('Failed', error);
}
}
}The two forms differ in what they give you:
| Form | What you receive | Use it when |
|---|---|---|
| Property | An object with `data` and `error` properties that the framework replaces on each update | The template only displays the result |
| Function | The same object, passed to your function on each update | You must run logic for each result |
In both forms the first value is { data, error }. Before the first response, data and error are both undefined. After a success, data holds the result and error is undefined. After a failure the reverse is true.
Rules that cause common errors
- Wired values are not available early. The wire runs after the component is created.
this.account.dataisundefinedin the constructor and often inconnectedCallback. Read it in a getter, in the template, or in the wired function. - Data is read-only. The object you receive is frozen because other components share it through the cache. Assigning to
this.account.data.fieldsfails. Copy the values you need into your own objects instead. - The property is replaced, not changed. The framework assigns a new object to
accounton every update. Readingthis.account.datainside a getter works because LWC re-renders when the property changes. - Do not call the adapter. Writing
getRecord({ recordId })in a method does not read a record. Use@wire, or use an imperative function that the module documents as callable. - Import
wirefromlwc. Forgetting it produces a compile error about an unknown decorator. - A property value that starts with
$is a reference.'$recordId'means "readthis.recordId". Without the$, the string itself is passed to the adapter. The next chapters return to this.
Wire or imperative call
Use @wire to read data that should stay in sync with the cache. Use an imperative function for actions that run once, such as saving. updateRecord returns a promise and runs only when you call it. Wired readers of the same record still receive the new values afterward.
Exercise
A component wires getRecord to a function, and the function reads this.account.data inside connectedCallback to log the name. The log shows undefined. Why, and where should the code go?
Show answer
The wire service provisions data after the component connects, so no result exists yet in connectedCallback. Put the logic in the wired function, which runs each time a result arrives, or read the value in a getter used by the template.