Sub topic 5
Observing nested changes with @track
This chapter assumes you know how a Lightning web component is structured (HTML, JS, and meta files), basic modern JavaScript (classes, spread syntax), and the difference between primitive values and object references. You should also know that a class field is reactive, and that reassigning a field rerenders the template.
The problem: mutation is invisible
A plain field is reactive when you reassign it. The component compares the field's old value to the new one, and if they differ, it rerenders. That works well for primitives such as numbers and strings.
Objects and arrays are different. The field holds a reference to the object. If you change a property inside the object, the reference stays the same, so a plain field does not notice the change.
import { LightningElement } from 'lwc';
export default class ProfileCard extends LightningElement {
profile = { name: 'Ada', city: 'London' };
rename() {
this.profile = { ...this.profile, name: 'Grace' };
}
}Here rename builds a new object with the spread operator and assigns it to profile. The reference changes, so the template refreshes. This is the preferred pattern, and it needs no decorator.
Observing nested changes with @track
Sometimes you want to write this.profile.name = 'Grace' and still see the template update. The @track decorator makes that work. It tells the framework to observe changes to the properties of a plain object or the elements of an array held in the field.
import { LightningElement, track } from 'lwc';
export default class TrackedProfile extends LightningElement {
@track profile = { name: 'Ada', city: 'London' };
rename() {
this.profile.name = 'Grace';
}
}Import track from lwc next to LightningElement. Then place @track directly before the field. A template that reads {profile.name} rerenders after rename runs.
<template>
<lightning-button label="Rename" onclick={rename}></lightning-button>
<p>{profile.name}, {profile.city}</p>
</template>How the two approaches compare
| Approach | Code | Rerenders? |
|---|---|---|
| Reassign, no decorator | `this.profile = { ...this.profile, name: 'Grace' }` | Yes |
| Mutate, no decorator | `this.profile.name = 'Grace'` | No |
| Mutate, with `@track` | `this.profile.name = 'Grace'` | Yes |
Arrays work the same way
The same rule applies to arrays. Without @track, calling push on an array field does not rerender the template. You can either reassign the field or track it.
import { LightningElement, track } from 'lwc';
export default class TodoList extends LightningElement {
items = ['Write', 'Test'];
@track tracked = ['Write', 'Test'];
addReassign() {
this.items = [...this.items, 'Ship'];
}
addMutate() {
this.tracked.push('Ship');
}
}Both methods update the list the template displays, but through different mechanisms.
What @track does not do
@track is not a general-purpose switch for reactivity, so keep these limits in mind:
- It does not make a primitive field more reactive. A number or string field already rerenders on reassignment, so
@trackadds nothing. - It observes only plain objects and arrays. Other kinds of objects, such as a
Date, aMap, or an instance of a custom class, are not observed deeply. - It observes only what the template actually reads. Changing a property that the template never uses does not cause a visible update.
The next chapter covers these limits in more detail.
Summary of when to use it
Start with a plain field. If you can express an update as a reassignment, do that. Add @track only when you have chosen to mutate the properties of a plain object or the elements of an array and need the template to follow along.
Exercise
A component declares profile = { name: 'Ada' }; with no decorator. A button handler runs this.profile.name = 'Grace';, and the template shows {profile.name}, but the page still shows "Ada". Give two different fixes.
Show answer
Fix 1: reassign the field with a new object, for example this.profile = { ...this.profile, name: 'Grace' };. Fix 2: add @track to the field declaration (@track profile = { name: 'Ada' };, importing track from lwc) so the mutation of name is observed. Reassignment is the preferred fix.
Exercise
Why does @track give no benefit on a field like count = 0?
Show answer
A primitive field is already reactive when reassigned (this.count += 1). @track only adds observation of changes inside plain objects and arrays, and a number has no inner properties to observe.