Sub topic 7
One-way data flow: the child does not change what it receives
This chapter assumes you can write a basic Lightning web component and that you already know how to expose a field with @api and set it from a parent template. Here you will learn the rule that governs what the child may do with that value.
The rule
Data in Lightning web components moves in one direction. The parent owns the value and passes it down. The child reads it. If the child needs a different value, it asks the parent to change it, and the parent decides what to do.
A public property is read-only from the child's point of view. The child can display it, compute from it, and copy it. It must not assign to it.
Why the rule exists
If a child could overwrite what it received, two components would each believe they own the same data. The parent would render one value, the child another, and neither would know why. One owner per value keeps the source of any bug easy to find: look at the component that owns the data.
What breaks
Consider a child that tries to change its own public property.
// taskCard.js (child)
import { LightningElement, api } from 'lwc';
export default class TaskCard extends LightningElement {
@api priority = 'normal';
handleEscalate() {
// Wrong: the parent owns this value.
this.priority = 'high';
}
}When a parent has set priority, this assignment is not a supported way to change it. The framework can log an error in the console, and the parent's next render can overwrite the child's change. The result is a card that shows one thing while the parent's data says another.
The default value in the example above is fine. A child may give a public property an initial value. The problem starts when it reassigns that property after the parent has set it.
The fix: keep your own state
If the child needs a value it can change, it copies the incoming value into a private field and changes the copy.
// taskCard.js (child)
import { LightningElement, api } from 'lwc';
export default class TaskCard extends LightningElement {
@api priority = 'normal';
_escalated = false;
get displayPriority() {
return this._escalated ? 'high' : this.priority;
}
handleEscalate() {
this._escalated = true;
}
}<!-- taskCard.html (child) -->
<template>
<p>Priority: {displayPriority}</p>
<button onclick={handleEscalate}>Escalate</button>
</template>The public property stays untouched. The private field _escalated belongs to the child, so the child may change it freely. Fields in a component class are reactive, so changing _escalated re-renders the template.
This card now shows "high" locally, but the parent still believes the priority is normal. If the parent must know, the child has to tell it.
Telling the parent
The child sends a custom event, and the parent updates its own data and passes the new value back down. Events are covered in detail in a later chapter. The shape is this:
handleEscalate() {
this.dispatchEvent(new CustomEvent('escalate'));
}The parent listens with onescalate={handleEscalate}, changes its data, and the new value flows down through the same property. The data makes a full loop, but each step goes in one direction.
Objects and arrays
The rule also applies to the contents of objects and arrays. A child that receives an object cannot change its fields, and a child that receives an array cannot push to it or sort it in place. Copy first, then change the copy. The next chapter covers this.
Summary of responsibilities
| Component | May do | Must not do |
|---|---|---|
| Parent | Own the data, change it, pass it down | Reach into the child's private fields |
| Child | Read public properties, copy them, keep private state | Reassign a public property set by the parent |
Exercise
A child component has @api label;. A button handler in the child runs this.label = this.label.toUpperCase();. Why is this a problem, and how would you show an uppercase label without it?
Show answer
The child is reassigning a public property whose value the parent owns, which breaks one-way data flow and can be overwritten when the parent re-renders. Instead, add a getter such as get upperLabel() { return this.label ? this.label.toUpperCase() : ''; } and bind {upperLabel} in the template. If the child must keep a changed value, copy it into a private field and change that.