Sub topic 5
What causes a component to render again
This chapter assumes you can write a basic Lightning Web Component (LWC) with an HTML template and a JavaScript class. It also assumes you know how template expressions like {count} read class fields. You should know which hooks run first: constructor(), connectedCallback(), render(), and renderedCallback().
Rendering is driven by data
After the first render, the framework doesn't re-render on a timer or on every event. It re-renders when data the template depends on changes. During a render, LWC records which reactive values the template read. If one of those values changes later, the component is queued to render again.
On a re-render, render() runs again and the framework updates only the parts of the DOM that differ. Then renderedCallback() runs. constructor() and connectedCallback() do not run again, because the component instance already exists and is still in the DOM.
What triggers a re-render
| Change | Re-renders? |
|---|---|
| A field read by the template is assigned a new value | Yes |
| A field is assigned the value it already holds | No |
| A field the template never reads is changed | No |
| A nested property of an object, or an array element, is mutated, and the field is not tracked | No |
| The same mutation, on a field decorated with `@track` | Yes |
| A parent passes a new value to an `@api` property that the template reads | Yes |
| A getter used in the template depends on a field that changed | Yes |
Two rules explain most of this table.
- Fields are reactive, but LWC compares by identity. Assigning a new object or array is a change. Editing an existing one is not, unless you use
@track, which makes LWC observe the object's properties and array elements. - Only values the template actually read count. A field used only inside event handlers can change freely without a re-render.
A worked example
This component shows four kinds of change.
<template>
<p>Count: {count}</p>
<p>Status: {filters.status}</p>
<p>Tags: {tagText}</p>
<lightning-button label="Increment" onclick={increment}></lightning-button>
<lightning-button label="Close filter" onclick={closeFilter}></lightning-button>
<lightning-button label="Add tag" onclick={addTag}></lightning-button>
<lightning-button label="Touch hidden" onclick={touchHidden}></lightning-button>
</template>import { LightningElement, track } from 'lwc';
export default class RenderDemo extends LightningElement {
count = 0;
hidden = 0;
tags = [];
@track filters = { status: 'open' };
get tagText() {
return this.tags.join(', ');
}
renderedCallback() {
console.log('rendered');
}
increment() {
this.count += 1; // new value, read by the template: re-renders
}
closeFilter() {
this.filters.status = 'closed'; // works because filters is tracked
}
addTag() {
this.tags.push('new'); // mutation, not tracked: no re-render
this.tags = [...this.tags, 'extra']; // new array: re-renders
}
touchHidden() {
this.hidden += 1; // never read by the template: no re-render
}
}Clicking Increment logs rendered once. Clicking Touch hidden logs nothing, since the template never reads hidden. In addTag, the push call alone would leave the page unchanged. The reassignment on the next line is what triggers the render, and the getter tagText then shows both tags.
Re-renders and renderedCallback
Every re-render ends with renderedCallback(). If that hook changes a value the template reads, it queues another render, which calls the hook again, and so on. That is the render loop covered under the pitfalls chapter.
renderedCallback() {
this.count += 1; // changes a value the template reads: loops forever
}Batching also matters. If a handler changes three fields in a row, LWC does not render three times. It renders once after the current code finishes. Don't expect the DOM to reflect a change on the line right after you make it.
Debugging an unexpected render or a missing one
If the screen doesn't update, check these in order:
- Does the template, or a getter it uses, read the value you changed?
- Did you mutate an object or array without
@track? If so, assign a new one. - Did you assign the same primitive value, or the same object reference?
If the component renders too often, log inside renderedCallback() and look for a template-read value being set there, or for a parent that passes a new object to an @api property on every render.
Exercise
A component has items = [] and a template that loops over items. A button handler calls this.items.push(task), but the list on screen doesn't change. Give two ways to fix it.
Show answer
Reassign the field with a new array, for example this.items = [...this.items, task]. Alternatively, decorate items with @track so LWC observes the array's elements. Reassignment is usually preferred because it works without the decorator and makes the change explicit.