Sub topic 6
What @track does not observe
This chapter assumes you know the following:
- Fields are reactive in a Lightning web component (LWC).
- Reassigning a field rerenders the template.
@trackmakes changes inside plain objects and arrays visible to the template.
The previous chapter showed what @track adds. This one covers where it stops. Most "my template did not update" bugs involving @track come from the cases below.
What @track actually does
When you decorate a field with @track, LWC wraps the object or array stored in that field in a reactive proxy. A proxy is an object that sits in front of another object and sees every property read and write that goes through it. During rendering, the template and any getters it calls read properties through the proxy. LWC records which properties were read. If code later writes to one of those properties through the proxy, the component rerenders.
This describes where the limits come from:
- Only reads and writes that pass through the proxy are seen.
- Only values that LWC chooses to wrap get a proxy.
- Only properties that rendering actually read matter.
The sections below follow from these three points.
Only plain objects and arrays are wrapped
LWC wraps two kinds of values: plain objects and arrays. A plain object is one created with an object literal ({}), Object.create(null), or JSON.parse. Everything else is left alone, even if it is stored inside a tracked object. The common cases are:
| Value | Wrapped by @track? | How to update it so the template refreshes |
|---|---|---|
| Plain object `{ a: 1 }` | Yes | Mutate a property, or reassign |
| Array `[1, 2]` | Yes | Mutate in place, or reassign |
| `Date` | No | Assign a new `Date` |
| `Map`, `Set` | No | Assign a new `Map` or `Set` |
| Instance of your own class | No | Assign a new instance, or keep plain data instead |
| DOM element | No | Not data. Do not mutate it expecting a rerender |
The fields that hold these values are still reactive. Reassigning the field (or the tracked property that holds the value) always works. What fails is mutating the value in place.
Example: a Date inside a tracked object
import { LightningElement, track } from 'lwc';
export default class Reminder extends LightningElement {
@track task = { title: 'Send report', due: new Date(2026, 11, 1) };
get dueText() {
return this.task.due.toDateString();
}
postponeInPlace() {
// Not observed: setDate changes the Date object itself.
this.task.due.setDate(this.task.due.getDate() + 1);
}
postponeByAssignment() {
// Observed: this writes the property `due` on a tracked plain object.
const next = new Date(this.task.due);
next.setDate(next.getDate() + 1);
this.task.due = next;
}
}<template>
<p>{task.title} is due {dueText}</p>
<lightning-button label="Postpone (in place)" onclick={postponeInPlace}></lightning-button>
<lightning-button label="Postpone (assign)" onclick={postponeByAssignment}></lightning-button>
</template>Click the first button and the stored date changes, but the paragraph stays the same. The proxy never sees setDate, because setDate changes the internal state of the Date and does not write a property on the task object. Click the second button and the paragraph updates. The first click's change then appears too, because the template reads the current Date.
Example: Map and Set
Map and Set follow the same rule. Methods such as set, add, delete, and clear change hidden internal storage, not properties.
import { LightningElement } from 'lwc';
export default class TagList extends LightningElement {
tags = new Set(['lwc']);
get tagText() {
return Array.from(this.tags).join(', ');
}
addTagInPlace() {
// Not observed: no field was reassigned and Set is not wrapped.
this.tags.add('apex');
}
addTag() {
// Observed: the field gets a new value.
this.tags = new Set([...this.tags, 'apex']);
}
}<template>
<p>Tags: {tagText}</p>
<lightning-button label="Add (in place)" onclick={addTagInPlace}></lightning-button>
<lightning-button label="Add (new Set)" onclick={addTag}></lightning-button>
</template>Adding @track to tags would change nothing here. @track only helps with plain objects and arrays.
Example: a class instance
An object created with new from your own class is not plain, so it is not wrapped, even inside a tracked field.
import { LightningElement, track } from 'lwc';
class Cart {
total = 0;
add(amount) {
this.total += amount;
}
}
export default class CartSummary extends LightningElement {
@track cart = new Cart();
addInPlace() {
// Not observed: Cart instances are not plain objects.
this.cart.add(5);
}
addByCopy() {
// Observed: the field is reassigned.
const next = new Cart();
next.total = this.cart.total;
next.add(5);
this.cart = next;
}
}<template>
<p>Total: {cart.total}</p>
<lightning-button label="Add 5 (in place)" onclick={addInPlace}></lightning-button>
<lightning-button label="Add 5 (copy)" onclick={addByCopy}></lightning-button>
</template>Often the better fix is to keep the state as plain data, such as { total: 0 }, and put the logic in component methods. Plain data works with @track and with reassignment. Class instances need you to copy them every time.
Mutating the original reference bypasses the proxy
Assigning an object to a tracked field does not turn your original variable into a proxy. The field holds a proxy, and your variable still points at the raw object underneath. Writes through the raw reference never reach the proxy.
import { LightningElement, track } from 'lwc';
export default class Draft extends LightningElement {
@track form = { title: '' };
reset() {
const fresh = { title: 'Untitled' };
this.form = fresh;
fresh.title = 'Changed later'; // Not observed.
}
resetCorrectly() {
this.form = { title: 'Untitled' };
this.form.title = 'Changed later'; // Observed: goes through the proxy.
}
}In reset, the assignment this.form = fresh triggers a rerender. The later write fresh.title = ... skips the proxy, so the template does not learn about it. The data does change, and the next rerender will show it. The rule is simple: after storing an object in a tracked field, do all further changes through this.form, not through the old variable.
The same problem appears with arrays you keep a second reference to, for example one held in a module-level variable or passed to a helper function that keeps it.
Only properties that rendering read are watched
The proxy notifies LWC only about properties that were read during the last render. If the template shows profile.name and your code changes profile.city, nothing rerenders, and that is correct. It is not a bug, but it can look like one when the change should be visible through a path you did not expect.
import { LightningElement, track } from 'lwc';
export default class Address extends LightningElement {
@track address = { street: '1 Main St', city: 'London' };
get label() {
return this.address.street;
}
changeCity() {
// No rerender: nothing in the last render read `city`.
this.address.city = 'Leeds';
}
}<template>
<p>{label}</p>
</template>This is a performance feature. If the template later starts reading city, for example inside an if:true branch, the property is watched from that render onward.
Reads that happen outside rendering are not recorded either. If you copy a value in a method (const c = this.address.city) and use c later in a template through a different field, only that other field's reassignment can update the view.
Read-only values cannot be tracked or mutated
Some objects arrive already protected. Values passed from a parent through an @api property, and data returned by wire adapters, are read-only. Writing to a property of such an object throws an error in the console. Wrapping it in @track does not make it writable. If you need to change data from a parent or a wire, make a copy first:
import { LightningElement, api, track } from 'lwc';
export default class EditableRow extends LightningElement {
@track draft;
_record;
@api
get record() {
return this._record;
}
set record(value) {
this._record = value;
this.draft = { ...value };
}
rename() {
this.draft.name = 'Edited';
}
}The setter copies the incoming record into a plain object. That copy is a shallow copy, so it is only safe to mutate its top-level properties. A nested object inside it is still the read-only original. Copy nested levels too, or reassign them, before changing them.
Deciding whether a value is observed
The diagram summarizes the questions to ask when a mutation does not rerender.
Practical guidance
- Treat reassignment as the default. It works for every kind of value and needs no decorator.
- Use
@trackonly for plain objects and arrays that you really want to edit in place. - Never rely on in-place methods of
Date,Map,Set, or custom classes to update the view. Create a new value and assign it. - After assigning an object to a tracked field, forget the original variable.
- Copy read-only data before editing it.
Exercise
A component declares @track order = { id: 1, items: [], placedOn: new Date(2026, 0, 1) };. The template shows {order.items.length} and {placedText}, where placedText is a getter that returns this.order.placedOn.toDateString(). A button handler runs this.order.items.push('pen'); this.order.placedOn.setFullYear(2027);. Which of the two displayed values updates, and why? How would you change the code so both are correct?
Show answer
The item count updates. items is an array under a tracked field, the handler writes through this.order, and the template read items.length, so the push is observed and the component rerenders. The year change alone would not have caused a rerender, because Date is not wrapped and setFullYear changes the date's internal state without writing a property. In this handler it still appears, but only because the rerender triggered by the push reads the current date. To make the date update reliably on its own, assign a new Date: const d = new Date(this.order.placedOn); d.setFullYear(2027); this.order.placedOn = d;. That write goes through the tracked proxy and is observed.
Exercise
A handler does const next = { name: 'Grace' }; this.person = next; next.name = 'Hopper';, where person is a @track field read by the template. Why might the template not show "Hopper" right away, and what is the fix?
Show answer
The field holds a reactive proxy around the object, but next is a reference to the raw object. Writing next.name bypasses the proxy, so LWC is not notified. Do the second write through the field: this.person.name = 'Hopper';, or build the final object first and assign it once: this.person = { name: 'Hopper' };.