Sub topic 5
How updates flow from parent to child
This chapter assumes you can build a simple Lightning web component and use {expression} bindings in a template. It also assumes you know how to expose a child property with @api and set it from a parent template. Here you will see what happens after that first render, when the parent's data changes.
The working model
When a parent binds a value to a child's public property, the link stays live. The parent does not hand over a one-time copy. Each time the bound value changes, the framework sets the child's property again and re-renders the child if the child's template uses that property.
You do not write code to push the update. You change the value in the parent, and the framework does the rest.
A worked example
The child displays a heading it receives from the parent.
// taskCard.js (child)
import { LightningElement, api } from 'lwc';
export default class TaskCard extends LightningElement {
@api heading;
}<!-- taskCard.html (child) -->
<template>
<h3>{heading}</h3>
</template>The parent binds nextHeading to the child and changes it when a button is clicked.
<!-- taskBoard.html (parent) -->
<template>
<lightning-button label="Rename" onclick={handleRename}></lightning-button>
<c-task-card heading={nextHeading}></c-task-card>
</template>// taskBoard.js (parent)
import { LightningElement } from 'lwc';
export default class TaskBoard extends LightningElement {
nextHeading = 'Write documentation';
handleRename() {
this.nextHeading = 'Review documentation';
}
}After the click, the child shows "Review documentation". The parent only assigned a new value to its own field.
What triggers an update
Fields in a component class are reactive. The framework tracks reassignment of a field that the template reads. For a primitive value such as a string, number, or boolean, assigning a different value is enough.
The update has two steps:
- The parent re-renders because a field its template uses has changed.
- During that render, the framework compares the new value with the one the child already has. If it differs, the framework sets the child's
@apiproperty, and the child re-renders.
If the new value is identical to the old one, nothing is set and the child does not re-render.
Objects and arrays
Changes to objects and arrays need more care. Reassigning the field always works:
// taskBoard.js (parent)
import { LightningElement } from 'lwc';
export default class TaskBoard extends LightningElement {
tasks = [{ id: 1, name: 'Write documentation' }];
handleAddTask() {
// New array, so the change is detected and the child receives it.
this.tasks = [...this.tasks, { id: 2, name: 'Review documentation' }];
}
}Mutating a value in place, such as calling push on the array, may not update every part of the page the way you expect. Create a new array or object and assign it to the field. This also makes it clear to a reader that the data has changed.
Chapter 8 covers passing objects and arrays in detail, including why the child should copy them.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Child never changes after the first render | The parent changed a value that is not bound to the child | Bind the field with `{field}` in the child's tag |
| Child shows the literal text `{nextHeading}` | The attribute value is wrapped in quotes | Write `heading={nextHeading}` without quotes |
| Child still shows old data after the parent edits a list | The list was changed in place | Assign a new array to the field |
| Child's own changes disappear on the next parent update | The child overwrote a property it received | Keep a private copy in the child |
Exercise
A parent holds tasks = [...] and passes it to a child with tasks={tasks}. A handler calls this.tasks.push(newTask), and the child's list does not show the new task. What should the handler do instead, and why?
Show answer
Assign a new array, for example this.tasks = [...this.tasks, newTask];. The framework detects that the field now holds a different value and sets the child's public property again, which re-renders the child. An in-place push does not reliably give the child a new value.
Exercise
In the worked example, the parent sets nextHeading to the same string it already holds. Does the child re-render? Explain.
Show answer
No. The value is identical to what the child already has, so the framework does not set the property again and there is nothing to re-render.