Sub topic 3
What actually triggers a rerender
This chapter assumes you know basic modern JavaScript (classes, spread syntax), the structure of a Lightning web component (HTML, JS, and meta files), and the difference between primitive values and object references. It also assumes you know that class fields are reactive by default, which the previous chapter covered.
Here you will learn the exact conditions that make a component rerender, and the conditions that do not.
The working model
A component rerenders when the framework sees that a field used by the template has a new value. The framework does not rerender after every method call or every event. It cares about one thing: did a field that the template reads get assigned a different value?
This gives three rules:
- The field must be read by the template, directly or through a getter that the template uses.
- The field must be assigned a new value, using
this.field = .... - The new value must differ from the old one. Assigning the same primitive, or the same object reference, does nothing.
The framework compares values with strict equality (===). Reassigning a field to an equal primitive is a no-op. Reassigning it to a different object is a change, even if both objects hold identical data.
A field the template reads
The Counter component below changes count and the template shows it.
import { LightningElement } from 'lwc';
export default class Counter extends LightningElement {
count = 0;
increment() {
this.count += 1;
}
}<template>
<lightning-button label="Add one" onclick={increment}></lightning-button>
<p>Count: {count}</p>
</template>this.count += 1 is an assignment. The value moves from 0 to 1, the template reads count, so the component rerenders and the paragraph updates.
What the template must read
A field that the template never reads does not cause a rerender, even if you change it. Consider a counter of clicks kept only for internal bookkeeping:
import { LightningElement } from 'lwc';
export default class ClickLogger extends LightningElement {
clicks = 0;
label = 'Clicked';
handleClick() {
this.clicks += 1;
}
}<template>
<lightning-button label="Click" onclick={handleClick}></lightning-button>
<p>{label}</p>
</template>The template reads only label. clicks changes on every click, but nothing on screen depends on it, so no rerender happens. This is a feature: unused fields cost nothing.
Getters count too
A getter that the template calls reads fields internally. If one of those fields changes, the component rerenders. The PriceTag component below uses this pattern.
import { LightningElement } from 'lwc';
export default class PriceTag extends LightningElement {
amount = 0;
get formatted() {
return this.amount.toFixed(2);
}
raise() {
this.amount += 1.5;
}
}<template>
<lightning-button label="Raise" onclick={raise}></lightning-button>
<p>Price: {formatted}</p>
</template>The template reads formatted. While computing it, the getter reads amount. Changing amount therefore rerenders the component.
The decision flow
Rerenders are batched
Several assignments in the same method do not cause several rerenders. The framework schedules one rerender after the current code finishes, and it uses the final values.
import { LightningElement } from 'lwc';
export default class Batch extends LightningElement {
first = 'A';
second = 'B';
swap() {
this.first = 'X';
this.second = 'Y';
// One rerender happens after swap() returns, showing X and Y.
}
}Common errors
| Symptom | Likely cause |
|---|---|
| The field changes in the debugger but the screen does not update | The template does not read the field, or the code mutated an object instead of reassigning it |
| A rerender does not happen after assigning the same value | The new value is strictly equal to the old one |
| An object field changes but the template shows old data | A property inside the object was changed, which is a mutation and not a reassignment |
The last row is the most common error. Changing this.profile.name does not assign this.profile, so the field itself has not changed. The next chapter covers reassignment versus mutation in detail.
Exercises
Exercise
A component has the fields total = 0 and lastUpdated = ''. The template shows only {total}. A method runs this.lastUpdated = 'now'. Does the component rerender, and why?
Show answer
No. The template does not read lastUpdated, so assigning it does not trigger a rerender. Only total is read by the template.
Exercise
A method runs this.count = 5 when count is already 5, and the template reads count. Does the component rerender?
Show answer
No. The new value is strictly equal to the old one, so the framework sees no change.