Sub topic 5
How updates flow from parent to child
This chapter explains what happens between a change in a parent component's data and the moment the child shows it. It assumes you can already expose a property with @api and set it from a parent template, as covered in earlier chapters. It also assumes you know the {expression} syntax in templates.
The core rule: reassignment triggers a render
Every field in a Lightning web component class is reactive. When the template reads a field and your code later assigns a new value to that field, the framework schedules a re-render of that component.
When the parent's template passes {value} to a child's @api property, the parent's re-render also updates that child property. The child re-renders if it uses that property, directly or through a getter, in its template.
The chain has four steps:
- Code in the parent assigns a new value to a field.
- The framework notices that a field the parent's template reads has changed.
- The parent re-renders. The framework compares the new bindings to the old ones, and sets the child's property if its value changed.
- The child re-renders if the property it received is used in its template.
You never call a "refresh" method. You change the data, and the framework does the rest.
A worked example
The parent owns a counter and a button. The child displays a label and the count.
// counterPanel.js (parent)
import { LightningElement } from 'lwc';
export default class CounterPanel extends LightningElement {
count = 0;
handleIncrement() {
this.count = this.count + 1;
}
}<!-- counterPanel.html (parent) -->
<template>
<lightning-button label="Add one" onclick={handleIncrement}></lightning-button>
<c-count-display label="Clicks" value={count}></c-count-display>
</template>// countDisplay.js (child)
import { LightningElement, api } from 'lwc';
export default class CountDisplay extends LightningElement {
@api label;
@api value = 0;
renderedCallback() {
console.log(`CountDisplay rendered with value ${this.value}`);
}
}<!-- countDisplay.html (child) -->
<template>
<p>{label}: {value}</p>
</template>Each click assigns a new number to count. The parent re-renders, sees that value={count} now holds a different number, and sets value on the child. The child's template reads value, so the child re-renders and logs a new line from renderedCallback.
The label property does not change, so the framework does not touch it again.
The sequence in detail
The last two steps matter for lifecycle code. A child's renderedCallback runs before its parent's renderedCallback, both on the first render and on later updates.
Updates are batched
The framework does not re-render on every assignment. It collects changes and re-renders once, after your current JavaScript finishes running. Consider this handler:
handleReset() {
this.count = 10;
this.count = 20;
this.count = 30;
}The child receives only 30. Its setter, if it has one, is not called with 10 or 20. Do not write child logic that expects to see every intermediate value.
The framework also compares the new value to the old one. If the parent assigns a value that is identical to the current one, the child gets no update. For example, assigning this.count = 30 when count is already 30 does nothing visible.
What triggers an update and what does not
Reassigning a field always works. Changing something inside an object or array held by a field is less reliable.
| Parent code | Parent field holds | Child updated? |
|---|---|---|
| `this.count = this.count + 1` | A number | Yes |
| `this.name = 'Ada'` | A string | Yes |
| `this.task = { ...this.task, done: true }` | An object (new object assigned) | Yes |
| `this.items = [...this.items, newItem]` | An array (new array assigned) | Yes |
| `this.items.push(newItem)` | An array, no `@track` | No |
| `this.task.done = true` | An object, no `@track` | No |
The last two rows are the most common source of "my child does not update" bugs. A plain field only notices when the field itself receives a new value. The framework cannot see that you changed something inside the existing array or object.
The simple habit that avoids this is to build a new object or array and assign it. The spread syntax from the prerequisites makes that short.
// taskBoard.js (parent)
import { LightningElement } from 'lwc';
export default class TaskBoard extends LightningElement {
tasks = [
{ id: 't1', name: 'Write documentation', priority: 'high' }
];
handleAddTask() {
const id = `t${this.tasks.length + 1}`;
// Assign a new array so the template sees a changed field.
this.tasks = [...this.tasks, { id, name: 'New task', priority: 'normal' }];
}
}<!-- taskBoard.html (parent) -->
<template>
<lightning-button label="Add task" onclick={handleAddTask}></lightning-button>
<template for:each={tasks} for:item="task">
<c-task-card
key={task.id}
heading={task.name}
priority={task.priority}>
</c-task-card>
</template>
</template>After the click, the parent re-renders and the list contains one more child. Existing cards keep their data, and the framework creates only the new card. Because each card has a unique key, the framework can match old and new cards correctly.
The @track decorator makes the framework watch changes inside an object or array. It can work, but reassignment is easier to reason about and behaves the same in every case.
What the child sees
Setting a public property replaces the child's value. If the child defines an @api setter, the setter runs once for each value that actually arrives.
// priorityBadge.js (child)
import { LightningElement, api } from 'lwc';
export default class PriorityBadge extends LightningElement {
_level = 'normal';
changeCount = 0;
@api
get level() {
return this._level;
}
set level(value) {
this.changeCount += 1;
this._level = value ? String(value).toLowerCase() : 'normal';
}
get cssClass() {
return `badge badge-${this._level}`;
}
}<!-- priorityBadge.html (child) -->
<template>
<span class={cssClass}>{level} (updates: {changeCount})</span>
</template>The template reads level, cssClass and changeCount. These depend on _level and changeCount, so a new value from the parent updates what the user sees. The framework tracks which fields each render read, including reads done inside getters.
A child re-renders only if a field it reads has changed. If a child receives a property but never uses it in the template or in a getter the template calls, the child does not re-render. This is expected, not a bug.
Initial values and timing
The first render follows a fixed order:
- The parent's constructor and
connectedCallbackrun. - The parent's template renders.
- The framework creates the child and sets its public properties.
- The child's
connectedCallbackruns, then its template renders. - The child's
renderedCallbackruns, then the parent's.
Public properties are available in the child's connectedCallback, but not in its constructor. If the child needs to react to the first value, put the logic in connectedCallback or in a setter. For later changes, use a setter or a getter-based derived value. Do not copy a received value into another field in connectedCallback only, or that copy will go stale when the parent updates the property.
// Stale: runs once, never sees later updates from the parent.
connectedCallback() {
this.displayName = this.name.toUpperCase();
}
// Current: recomputed from the received value every render.
get displayName() {
return this.name ? this.name.toUpperCase() : '';
}Avoiding extra renders
Re-renders are cheap, but a large list of children that all update on every change can feel slow. Two practices help:
- Assign a field only when its value truly changes. An assignment to an identical primitive is ignored, but a new object that holds the same data is treated as a change.
- Split a big object into the smaller values a child needs. Passing
heading={task.name}instead oftask={task}means the child receives a new value only when that name changes.
Exercise
A parent has items = [] and runs this.items.push('a') in a click handler. Its template passes {items} to a child's @api items. The child never shows the new item. Why, and how do you fix it?
Show answer
A plain field only triggers a re-render when it is reassigned, and push changes the existing array without assigning the field. The fix is to assign a new array: this.items = [...this.items, 'a'];.
Exercise
A parent handler runs this.level = 'low'; this.level = 'high'; in one function. The child has an @api setter for level. How many times does the setter run for this click, and with which value?
Show answer
Once, with 'high'. The framework batches changes and re-renders after the handler finishes, so the child receives only the final value.
Exercise
A child copies this.title into this.upperTitle in connectedCallback. The parent later changes the title, but the child still shows the old text. What is the cause and a better design?
Show answer
connectedCallback runs once, so the copy is made only from the first value. Replace the copied field with a getter such as get upperTitle() { return this.title ? this.title.toUpperCase() : ''; }, or do the transformation in an @api setter.