Sub topic 2
How Updates Flow from Parent to Child
Assume you know basic HTML, modern JavaScript, and how Lightning web component templates use expressions such as {name}.
The update path
A parent owns its state. When that state changes, the parent rerenders and sends new values to the child’s public properties. The child rerenders when those properties change.
This flow is automatic when the parent binds a value:
<!-- taskBoard.html -->
<template>
<c-task-card
heading={taskName}
show-details={detailsVisible}>
</c-task-card>
</template>If taskName or detailsVisible changes in the parent, the child receives the new values.
Boolean properties default to false
A Boolean public property represents an on-or-off setting. Give it a false default so the component is disabled unless the parent enables it.
// taskCard.js
import { LightningElement, api } from 'lwc';
export default class TaskCard extends LightningElement {
@api showDetails = false;
}In markup, show-details maps to showDetails. Do not write show-details="false" when you mean false. An attribute’s presence can be treated as enabled. Bind a real Boolean value instead:
| Parent markup | Meaning |
|---|---|
| `show-details={detailsVisible}` | Uses the parent’s Boolean value |
| `show-details` | Enables the property |
| Omitted | Uses the child’s default, usually `false` |
One-way data flow
Data moves down from parent to child. The child must not change a received value and expect the parent to change with it. The parent remains the source of truth.
This matters most for arrays and objects. Copy an incoming array before sorting, filtering, or adding items. A shallow copy protects the array itself, though nested objects still share references.
// taskList.js
import { LightningElement, api } from 'lwc';
export default class TaskList extends LightningElement {
_tasks = [];
@api
get tasks() {
return this._tasks;
}
set tasks(value) {
this._tasks = Array.isArray(value) ? [...value] : [];
}
sortByName() {
this._tasks = [...this._tasks].sort((a, b) =>
a.name.localeCompare(b.name)
);
}
}Use an @api setter when input needs validation or transformation. For deeper nested changes, copy the affected object as well, rather than mutating the parent’s object.
Check your understanding
Exercise
Why should a child copy an array before sorting it?
Show answer
The array belongs to the parent. Copying it lets the child change its own list without mutating the parent’s data.