Sub topic 2
Every field is reactive
This chapter explains how Lightning web components (LWC) treat class fields. It assumes you know basic modern JavaScript (classes, import/export), basic HTML templates, and the three files of a component (HTML, JS, and meta). It also assumes you know the difference between a primitive value and an object reference.
The working model
In current LWC, every field in a component class is reactive. You don't need a decorator for this. When the framework renders a template, it records which fields the template reads. If one of those fields gets a new value later, the component rerenders and the template shows the new value.
Older tutorials say you must add @track to make a field update the page. That has been false since the Spring '20 release. Plain fields work.
A first example
This class has one field, count, and one method that changes it.
import { LightningElement } from 'lwc';
export default class Counter extends LightningElement {
count = 0;
increment() {
this.count += 1;
}
}The template reads count and calls increment when the button is clicked.
<template>
<lightning-button label="Add one" onclick={increment}></lightning-button>
<p>Count: {count}</p>
</template>this.count += 1 assigns a new value to the field. The framework sees the assignment, and the paragraph shows the new number. No decorator is involved.
What triggers a rerender
A rerender is triggered by a change to a field that the template used during its last render. Two details follow from this.
- The change must be a reassignment of the field, such as
this.count = 5. The framework compares the old and new value. If they are the same, nothing happens. - The field must be used by the template. If you change a field that no template expression reads, there is nothing to update, so no rerender is needed.
The flow looks like this.
Objects and arrays: reassign the field
A field can hold an object. Reassigning the field to a new object counts as a change, even if the new object has the same shape. A common way to build the new object is the spread operator (...), which copies the properties of an existing object into a new one.
import { LightningElement } from 'lwc';
export default class ProfileCard extends LightningElement {
profile = { name: 'Ada', city: 'London' };
rename() {
this.profile = { ...this.profile, name: 'Grace' };
}
}The right side builds a new object with name replaced. The assignment changes the value of profile, so the component rerenders. This works without @track.
The next chapters cover the other case, where code changes a property inside the existing object instead of assigning a new one. For now, remember the rule: if you reassign the field, the template updates.
Fields you don't need to worry about
- Fields that are never read by the template can be plain fields too. Reassigning them does not cause a rerender, and that is fine.
- Fields set to
undefinedornullare still reactive. Declare them (selectedId;) so the class shows what state it holds.
Common mistake
Adding @track to every primitive field "just in case". It does nothing for strings, numbers, or booleans. Leave the decorator off unless you need to observe changes inside an object or array, which a later chapter covers.
Exercise
A component has the field total = 10 and the template shows {total}. A click handler runs this.total = 10;. Does the component rerender, and why?
Show answer
No. The new value equals the old value, so the framework sees no change. A rerender happens only when the assigned value differs from the current one and the template read the field.
Exercise
A component has items = ['a', 'b'], and the template iterates over items. Write a handler line that adds 'c' so that the template updates without @track.
Show answer
this.items = [...this.items, 'c']; This assigns a new array to the field, which counts as a change. Calling this.items.push('c') would modify the array in place and would not be observed on a plain field.