Sub topic 4
Reactive fields: what triggers a re-render
This chapter assumes you can write a basic LWC bundle and know JavaScript classes. It explains when a change to a class field updates what the user sees, and when it does not.
Fields are reactive by default
Every field declared on a component class is reactive. When a field's value changes and the template uses that field, the component re-renders. You don't need a decorator for this. A decorator is the @ annotation, such as @api or @track.
Here is a counter that uses the pattern:
<!-- counter.html -->
<template>
<p>Clicked {count} times</p>
<lightning-button label="Add one" onclick={handleClick}></lightning-button>
</template>// counter.js
import { LightningElement } from 'lwc';
export default class Counter extends LightningElement {
count = 0;
handleClick() {
this.count = this.count + 1;
}
}Each click assigns a new number to count. The framework notices the assignment and updates the paragraph.
What the framework watches
The framework does not poll your fields. It records which fields the template reads while rendering. When one of those fields is assigned a different value, it schedules a new render.
Two consequences follow:
- A field the template never reads can change without causing any render. This is fine, because nothing on screen depends on it.
- A getter that the template reads counts too. Any field the getter reads is tracked as a dependency of that render.
Assigning the same value again, such as setting count = 5 when it is already 5, does not trigger a render.
flowchart TD
A[Handler assigns a field] --> B{Value different from before?}
B -- No --> F[Nothing happens]
B -- Yes --> C{Did the last render read this field?}
C -- No --> F
C -- Yes --> D[Re-render is scheduled]
D --> E[Template updates in the DOM]I'll replace that block with the required diagram format below.
Renders are batched
Re-rendering does not happen in the middle of your handler. The framework waits until the current JavaScript task finishes, then renders once. A handler can assign ten fields and still cause a single render.
// batchDemo.js
import { LightningElement } from 'lwc';
export default class BatchDemo extends LightningElement {
first = 'Ada';
last = 'Lovelace';
rename() {
this.first = 'Alan';
this.last = 'Turing';
// One render happens after rename() returns.
}
}The practical result is that you don't need to worry about intermediate states. The template only ever shows the values present when the handler finishes.
Objects and arrays: reference versus contents
A field holding an object or array is observed by reference. These changes are noticed:
- Assigning a new object or array to the field.
These changes are not noticed on a plain field:
- Setting a property on the object that the field holds.
- Calling
pushorspliceon the array that the field holds.
// addressCard.js
import { LightningElement } from 'lwc';
export default class AddressCard extends LightningElement {
address = { city: 'Paris' };
// Not observed on a plain field: the template stays on "Paris".
moveInPlace() {
this.address.city = 'Lyon';
}
// Observed: the field receives a new object.
moveByReplacing() {
this.address = { ...this.address, city: 'Lyon' };
}
}Replacing the object with a copy is the usual fix. The decorator @track is the other option, because it makes the framework observe changes inside the object. A later chapter covers it. Prefer replacement when you can, because it is easy to follow and works the same way for every kind of field.
Common mistakes
| Symptom | Likely cause | Fix |
|---|---|---|
| Screen does not change after `push` | Array changed in place | Assign a new array, for example `this.items = [...this.items, item]` |
| Screen does not change after setting `obj.x` | Object changed in place | Assign a copy of the object, or use `@track` |
| Value changes in the debugger but not on screen | Template never reads that field | Bind the field or a getter that reads it |
| Value updates only after the handler ends | Renders are batched | Expected behavior; don't rely on the DOM inside the handler |
Exercise
A component has items = ['Pen'] and the template shows {itemCount} from a getter returning this.items.length. A handler runs this.items.push('Notebook'). The screen still shows 1. Why, and how do you fix it?
Show answer
push changes the array in place, and a plain field is observed only by reference, so no re-render is scheduled. Assign a new array instead: this.items = [...this.items, 'Notebook'];. The getter then reads the new array and the screen shows 2.