Sub topic 5
Making the wire reactive with $recordId
This chapter assumes you can write a Lightning web component with an HTML template and a JavaScript class. It also assumes you know how @api recordId works on a record page, and that you have seen @wire(getRecord, ...) in the previous chapter. Here you will learn how the $ prefix makes a wire re-run when a value changes.
Static and reactive parameters
A wire adapter takes a configuration object. Each value in that object is either static or reactive.
- A static value is a literal, such as an array of field references. The wire reads it once.
- A reactive value is a string that starts with
$and names a property of the component. The wire reads that property and watches it.
import { LightningElement, api, wire } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Account.Name';
export default class AccountName extends LightningElement {
@api recordId;
@wire(getRecord, { recordId: '$recordId', fields: [NAME_FIELD] })
account;
get name() {
return getFieldValue(this.account.data, NAME_FIELD);
}
}In this example '$recordId' is not the record ID itself. It is a pointer to this.recordId. The wire service looks up that property and passes its current value to getRecord.
What happens when the value changes
When a reactive property changes, the wire service calls the adapter again with the new value. The wired property or function then receives a new result. You do not write any code to trigger this.
The sequence looks like this:
A common case is a component placed in a parent that lets the user pick a record. Each pick sets a new recordId, and the child shows the new record.
The undefined case
A reactive property is often undefined at first. On a record page, recordId is set after the component is created. The wire service does not call the adapter while a required reactive parameter is undefined. No request is made and no error is raised. Once the value is set, the adapter runs.
This means you do not need an if (this.recordId) guard around the wire. You do need to handle the empty state in the template, because account.data is undefined until the first result arrives.
<template>
<template lwc:if={account.data}>
<p>Name: {name}</p>
</template>
</template>Rules for reactive parameters
- The
$string must name a property on the component, written as'$propertyName'. - You can only use a top-level property. A path such as
'$record.id'is not supported in the form shown here. Copy the value into its own property or getter first. - A getter works as a reactive source. Use one when the value must be computed.
- Fields that are not prefixed with
$do not react, even if you later change the array they came from.
import { LightningElement, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
import NAME_FIELD from '@salesforce/schema/Account.Name';
export default class AccountPicker extends LightningElement {
selected;
@wire(getRecord, { recordId: '$selected', fields: [NAME_FIELD] })
account;
handlePick(event) {
this.selected = event.detail.recordId;
}
}Here selected starts as undefined, so no request is sent. After handlePick runs, the wire fires with the chosen ID.
Common errors
- Writing
recordId: this.recordId. The decorator runs when the class is defined, sothisis not your component. Use'$recordId'. - Forgetting the quotes.
recordId: $recordIdis a syntax error. - Expecting the wire to react to a change inside an object. Replace the property value instead.
- Assuming a wire runs once. It runs again each time a reactive value changes.
Exercise
A component declares @wire(getRecord, { recordId: '$selected', fields: [NAME_FIELD] }) account;. The selected property is undefined when the component loads. What does the wire do at that moment, and what happens when selected is later set to a valid record ID?
Show answer
While selected is undefined, the wire service does not call getRecord, so no request is made and no error appears, and account.data stays undefined. When selected is set to a valid ID, the wire service calls getRecord with that ID and the component receives the record in account.data, or an error in account.error.