Sub topic 6
What @track does not observe
This chapter assumes you know that every field is reactive on reassignment, and that @track observes changes inside plain objects and arrays. Here you will learn where that observation stops, so you can spot a template that fails to refresh and fix it.
What @track watches
@track wraps a field's value in a proxy, an object that intercepts reads and writes. The proxy lets the framework notice when you change a property. It only does this for two kinds of value:
- Plain objects, such as
{ name: 'Ada' }. - Arrays.
The proxy also covers nested plain objects and arrays inside them. Everything else is left alone. Reassigning the field itself still works for any value, tracked or not.
| Value stored in the field | Mutation observed with @track? | How to refresh |
|---|---|---|
| Plain object | Yes, including nested properties | Mutate or reassign |
| Array | Yes, including nested items | Mutate or reassign |
| `Date` | No | Reassign a new `Date` |
| `Map` or `Set` | No | Reassign a new `Map` or `Set` |
| Instance of your own class | No | Reassign a new instance |
Built-in objects are not observed
Adding @track to a field that holds a Date does nothing for mutations. The Date is not a plain object, so no proxy is created. Its methods change internal state that the framework cannot see.
import { LightningElement, track } from 'lwc';
export default class Deadline extends LightningElement {
@track due = new Date(2026, 0, 1);
get dueYear() {
return this.due.getFullYear();
}
// Does not rerender: setFullYear mutates the Date in place.
brokenAdvance() {
this.due.setFullYear(this.due.getFullYear() + 1);
}
// Rerenders: the field gets a new value.
advance() {
const next = new Date(this.due);
next.setFullYear(next.getFullYear() + 1);
this.due = next;
}
}Map and Set behave the same way. Calling set, add, or delete on them is invisible to the framework. Build a new collection and assign it:
import { LightningElement } from 'lwc';
export default class TagList extends LightningElement {
tags = new Set(['lwc']);
get tagText() {
return Array.from(this.tags).join(', ');
}
addTag(name) {
// this.tags.add(name) would not rerender.
this.tags = new Set(this.tags).add(name);
}
}No @track is needed here. Reassignment already triggers the rerender.
Class instances are not observed
An instance of your own class is not a plain object either. This surprises people who track a model object.
import { LightningElement, track } from 'lwc';
class Account {
constructor(name) {
this.name = name;
}
}
export default class AccountView extends LightningElement {
@track account = new Account('Acme');
brokenRename() {
this.account.name = 'Globex'; // no rerender
}
rename() {
this.account = new Account('Globex'); // rerender
}
}The fix is the same each time. Replace the value instead of changing it in place.
The template must read the property
Observation alone does not force a rerender. A change causes one only if the template, or a getter the template uses, read that property during the last render. Suppose a tracked object holds name and city, and the template shows only name. Mutating city leaves the page unchanged, and that is correct behavior.
Tracked values are proxies
Reading a tracked field returns the proxy, not the object you assigned. Two consequences follow:
- Comparing
this.profile === originalObjectisfalseeven though they wrap the same data. - Passing a tracked value to code that checks identity or needs a plain object can behave differently.
If you need a plain copy, build one explicitly, for example with { ...this.profile } or JSON.parse(JSON.stringify(this.profile)). The second form only suits data that is JSON-safe, so it drops functions and turns dates into strings.
Summary of the rule
Use reassignment as your default. It works for every kind of value. Reach for @track only when the value is a plain object or array and you prefer to mutate its properties directly.
Exercise
A component declares @track items = new Set(['a']); and calls this.items.add('b') in a click handler. The template lists the items, but the list does not update. Why, and what is the smallest fix?
Show answer
@track only observes plain objects and arrays, and a Set is neither, so add changes it without the framework noticing. Replace the value: this.items = new Set(this.items).add('b');. The @track decorator can then be removed, because reassignment alone triggers the rerender.