Sub topic 5
Reactive parameters with the $ prefix
This chapter assumes you can write a basic LWC with @api properties, that you know JavaScript destructuring, and that you have already wired an Apex method to a property. Here you will learn how a wire decides when to run again.
What "reactive" means for a wire
A wire does not run only once. The framework watches the values you give it as parameters. When one of them changes, the framework calls the Apex method again and provisions the new result.
You mark a parameter as reactive by passing a string that starts with $. The text after the $ names a property on the component class.
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;
}In this example, '$recordId' is not the literal text "$recordId". The framework reads this.recordId and sends that value as accountId. If recordId later changes, the wire runs again with the new value.
How the framework treats the parameters
Three rules cover most of the behavior you will see.
- A parameter that starts with
$is reactive. The framework looks it up on the component instance. - A parameter without
$is a fixed value, and it never triggers a new call. - If any reactive parameter is
undefined, the wire does not call Apex at all.
The third rule matters most in practice. When the component first loads, recordId may not be set yet. The wire waits, then runs once the value arrives.
Reactive values you can change yourself
The $ value does not have to be an @api property. It can be any field on the class. Fields are reactive for reassignment, so changing the field triggers the wire. This lets a user choice drive the call.
import { LightningElement, wire } from 'lwc';
import searchContacts from '@salesforce/apex/ContactController.searchContacts';
export default class ContactSearch extends LightningElement {
searchTerm = '';
@wire(searchContacts, { term: '$searchTerm' })
results;
handleChange(event) {
this.searchTerm = event.target.value;
}
}This example assumes an Apex method searchContacts(String term) exists with @AuraEnabled(cacheable=true). Each time handleChange assigns a new searchTerm, the wire calls Apex again.
Common errors
- Forgetting the
$. Writing{ accountId: 'recordId' }sends the literal stringrecordIdto Apex. For anIdparameter this usually produces an error. The wire will not react to changes. - Putting
$on a nested path in the wrong way. The$string can read a path such as'$filters.status', but the framework only sees a change when the object is reassigned. Changingthis.filters.status = 'Open'in place does not trigger the wire in a reliable way. Assign a new object instead:this.filters = { ...this.filters, status: 'Open' };. - Calling Apex on every keystroke. The search example above calls Apex once per change. For a text input, add a short delay (a debounce) before you assign the new value.
- Expecting a call when a value is
undefined. If the property isundefined, the wire stays idle andcontacts.datastaysundefined. Usenullor an empty string only if your Apex method handles it.
Using a getter for computed values
A getter can supply a computed value. The framework reads it like any other property.
import { LightningElement, api, wire } from 'lwc';
import getContacts from '@salesforce/apex/ContactController.getContacts';
export default class ContactListComputed extends LightningElement {
@api recordId;
get accountIdOrUndefined() {
return this.recordId ? this.recordId : undefined;
}
@wire(getContacts, { accountId: '$accountIdOrUndefined' })
contacts;
}Returning undefined from the getter keeps the wire idle until a real value exists.
Exercise
A component has @wire(getContacts, { accountId: 'recordId' }). Contacts never reload when the user opens a different account, and Apex receives an unexpected value. What is wrong, and how do you fix it?
Show answer
The parameter is missing the $ prefix, so the framework treats 'recordId' as a fixed string. It sends that literal text to Apex and never watches the recordId property. Change it to { accountId: '$recordId' } so the wire reads this.recordId and runs again whenever it changes.
Exercise
A component keeps its filters in this.filters = { status: 'New' } and wires with { status: '$filters.status' }. A button runs this.filters.status = 'Open', but the data does not update. How do you fix the button handler?
Show answer
Changing a property inside the object in place does not reliably signal a change to the wire. Assign a new object instead: this.filters = { ...this.filters, status: 'Open' };.