Sub topic 1
Recap: the wire service and reactive parameters
Prerequisites
This chapter assumes you already know the following:
- The structure of a Lightning web component: a JavaScript class, an HTML template, and a metadata file.
- Salesforce objects, fields, and relationships.
- The basic ideas of GraphQL: queries, fields, and arguments. Later chapters cover these in more detail.
If any of these are new to you, review them first. This chapter only refreshes the wire service so the GraphQL chapters can build on it.
What the wire service does
The wire service is a mechanism that connects a component to a data source. You declare what you want, and the platform provisions the data. It calls your code again when the inputs change.
You use it through the @wire decorator. The decorator takes an adapter and an optional configuration object. The adapter is the function that fetches the data. For the GraphQL adapter, the import is graphql from lightning/uiGraphQLApi. A later chapter introduces it.
You can wire to a property or to a function.
import { LightningElement, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
export default class WireDemo extends LightningElement {
@Prop recordId;
}The snippet above is only a shape. The complete, runnable version is below.
import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
export default class WireDemo extends LightningElement {
@api recordId;
@wire(getRecord, { recordId: '$recordId', fields: ['Account.Name'] })
account;
get name() {
return this.account?.data?.fields?.Name?.value;
}
}The wired property account receives an object with two keys, data and error. Before the first response, both are undefined. The GraphQL adapter returns data and errors instead. A later chapter covers that difference.
Reactive parameters
A configuration value that starts with $ is a reactive parameter. The string after the $ names a property on the component. In the example above, '$recordId' refers to this.recordId.
When the referenced property changes, the wire service provisions the data again. You do not call anything yourself. The component rerenders when the new value arrives.
Three rules matter:
- The
$prefix works only on top-level property names.'$record.id'is allowed, but expressions and function calls are not. - The value must be a string that starts with
$. A plain property reference without the quotes is evaluated once, when the decorator runs. - If the property is
undefined, the wire service does not call the adapter. It waits until the value is defined. This is a common way to delay a request.
The GraphQL adapter uses this same mechanism. You will pass variables: '$variables', where variables is a getter that returns an object. When any property that the getter reads changes, the query runs again.
The wire lifecycle
Wiring to a function
When you need to process the result, wire to a function. The function runs each time the adapter provides a new result.
import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
export default class WireFunction extends LightningElement {
@api recordId;
name;
error;
@wire(getRecord, { recordId: '$recordId', fields: ['Account.Name'] })
handleRecord({ data, error }) {
if (data) {
this.name = data.fields.Name.value;
this.error = undefined;
} else if (error) {
this.error = error;
this.name = undefined;
}
}
}The GraphQL examples in this tutorial use this function form, because they reshape the nested result into a flat list for the template.
Common errors
| Problem | Cause | Fix |
|---|---|---|
| The wire never fires | A reactive parameter is `undefined` | Make sure the property has a value, or confirm that the delay is intended |
| The wire does not rerun | The `$` prefix is missing, or you changed a nested object in place | Use `'$name'` and assign a new object instead of mutating the old one |
| Data is `undefined` at first render | The response is asynchronous | Guard with `if (data)` or optional chaining |
| Changing a property inside the wire function triggers a loop | The function updates a property that the wire reads | Keep result properties separate from input properties |
Exercise
A component wires an adapter with { accountId: '$selectedId' }. selectedId is undefined when the component loads, and the user picks an account later. What happens at load time, and what happens after the pick?
Show answer
At load time the wire service does not call the adapter, because a reactive parameter is undefined. After the user picks an account, selectedId gets a value, the wire service calls the adapter, and the component rerenders with the result.