Sub topic 9
Reactivity: how class fields update the UI
This chapter assumes you know the component bundle layout and template expressions such as {name} from the previous chapters. It also assumes you are comfortable with JavaScript classes and getters.
Fields drive the template
In LWC, a class field used in the template is reactive. When you assign a new value to it, the framework schedules a re-render of that component. You write no subscription code and no setState call.
// counter.js
import { LightningElement } from 'lwc';
export default class Counter extends LightningElement {
count = 0;
increment() {
this.count += 1;
}
get doubled() {
return this.count * 2;
}
}<!-- counter.html -->
<template>
<p>Count: {count}, doubled: {doubled}</p>
<button onclick={increment}>Add</button>
</template>Each click reassigns count. The template reads count and the getter doubled, so the component re-renders and both values update. A getter is the usual way to derive a value, because it is recomputed on every render.
What happens on assignment
The framework does not re-render synchronously on each assignment. It batches changes and renders once in a microtask.
If a handler changes three fields, the template still renders once. Code that runs right after an assignment sees the old DOM, because the update has not happened yet.
Objects and arrays: reassign, don't mutate
The framework observes assignment to the field itself. It does not observe changes inside a plain object or array stored in the field.
import { LightningElement } from 'lwc';
export default class TodoList extends LightningElement {
items = [{ id: 1, label: 'Write chapter' }];
addItem() {
// Not reactive: this.items.push({ id: 2, label: 'Review' });
this.items = [...this.items, { id: this.items.length + 1, label: 'Review' }];
}
}The commented push changes the array but never assigns the field, so the list stays stale. Creating a new array and assigning it triggers the re-render. The same applies to objects: use this.user = { ...this.user, name: 'Ada' }.
The @track decorator makes the framework watch nested changes inside an object or array. Prefer reassignment, because it is explicit and works the same way as the rest of the code.
Common errors
- Mutating a nested value or calling
pushand expecting a re-render. - Using a local variable or a plain function value instead of a class field. Only fields trigger renders.
- Assigning a field in
renderor in a getter. This can cause a render loop. - Reading the DOM immediately after assignment and finding the old content.
Standard or LWC?
Standard JavaScript has no field-change notification. This behavior comes from the LWC framework, which instruments the component class so assignments schedule a render.
Exercise
A handler runs this.items.push(newItem), where items is a plain array field rendered by for:each, and the list does not update. Why, and what is the fix?
Show answer
push mutates the array without assigning the field, so the framework sees no change. Assign a new array instead, for example this.items = [...this.items, newItem], or mark the field with @track.