Sub topic 10
Changing objects and arrays in place with @track
This chapter assumes you know how class fields trigger re-renders when you reassign them, and how a template binds to a field with { }. It covers what happens when you change something inside an object or array instead of replacing it.
Reassigning versus mutating
Every class field in an LWC component is reactive. If the template reads a field and your code assigns a new value to it, the component re-renders.
For a field that holds an object or array, the framework watches the field itself, meaning the reference stored in it. It does not watch the contents. Compare two operations:
this.address = { city: 'Lyon' };replaces the reference. The template re-renders.this.address.city = 'Lyon';changes a property inside the same object. The reference is unchanged, so a plain field does not re-render.
Observing in-place changes with @track
The @track decorator tells the framework to observe the contents of a plain object or array, including nested properties. Here is a component that uses it.
<!-- profile.html -->
<template>
<p>City: {address.city}</p>
<lightning-button label="Move to Lyon" onclick={moveToLyon}></lightning-button>
</template>// profile.js
import { LightningElement, track } from 'lwc';
export default class Profile extends LightningElement {
@track address = { city: 'Paris' };
moveToLyon() {
this.address.city = 'Lyon';
}
}Clicking the button changes address.city in place. Because address is decorated with @track, the template shows "Lyon". Without @track, the value would change in memory but the page would keep showing "Paris".
The same rule applies to arrays. With @track items = [], a call such as this.items.push('Pen') updates the template. Without it, the push is not noticed.
Here is the decision as a diagram.
Replacing instead of mutating
You can often skip @track by creating a new object or array and assigning it to the field. The spread syntax makes this short.
// cartList.js
import { LightningElement } from 'lwc';
export default class CartList extends LightningElement {
items = ['Pen'];
addNotebook() {
this.items = [...this.items, 'Notebook'];
}
renameCity() {
this.address = { ...this.address, city: 'Lyon' };
}
address = { city: 'Paris' };
}Both methods assign a new reference, so the template re-renders without @track. This is the usual choice when you can copy cheaply. It also makes each change an explicit assignment, which is easier to read and debug.
| Approach | Re-renders on in-place change | Notes |
|---|---|---|
| Plain field, reassign | Not applicable, always re-renders | Copy with spread or `map` and `filter` |
| Plain field, mutate | No | The value changes, but the page does not update |
| `@track` field, mutate | Yes | Observes nested properties and array elements |
Limits of @track
- It works only on plain objects and arrays. Instances of classes,
Date,Map, andSetare not observed deeply. - It adds observation overhead for the whole object tree, so apply it only to fields you really mutate.
- It affects only fields. It does nothing for a local variable inside a method.
- Reassigning a tracked field still works and still re-renders.
Choosing between them
Use @track when many handlers update parts of one nested structure and copying each time would be clumsy. Use reassignment when a change is a single, clear update, or when the data is passed to a child component, since a new reference is an unambiguous signal that something changed.
Exercise
A component declares items = ['Pen']; without @track, and its template loops over items. A handler runs this.items.push('Notebook');. The page does not update. Give two ways to fix it.
Show answer
Either decorate the field with @track (@track items = ['Pen'];), so the in-place push is observed, or replace the push with a reassignment, this.items = [...this.items, 'Notebook'];, which gives the field a new reference and re-renders without @track.