Sub topic 8
Why a child must not change its @api properties
This chapter assumes you know how a Lightning web component is laid out (HTML, JavaScript, and meta files). It also assumes you know how to expose a field with @api, and the difference between reassigning a field and mutating the object it holds.
The rule
A property decorated with @api is owned by the parent that sets it. Inside the child, the property is read-only. The child can read it in its template and in its methods, but it must not assign a new value to it.
This is the one-way data flow of Lightning Web Components. Data flows down from parent to child through public properties. Requests to change data flow up from child to parent through events.
What goes wrong
Consider a child that tries to change its own public property.
import { LightningElement, api } from 'lwc';
export default class InfoCard extends LightningElement {
@api heading;
@api expanded = false;
toggle() {
// Not allowed: the parent owns this value.
this.expanded = !this.expanded;
}
}The parent sets expanded in its template. If the child overwrites it, the child's value and the parent's value no longer agree. The parent does not know that the change happened, so its own state is now wrong. Parent and child can each show a different truth, and the bug is hard to trace.
LWC protects against this. When the parent has set a value from its template, assigning to the property inside the child is an error, and the framework reports it in the console (in development mode) instead of silently accepting it. Do not rely on that error as a design tool. Treat it as a sign that the data has the wrong owner.
The same rule covers objects. A value passed through @api is read-only for the child, including its nested properties. Mutating this.profile.name on a received object is also unsafe, because the object belongs to the parent.
The correct pattern: copy, or ask the parent
You have two correct options, depending on who should own the state.
Option 1: Keep a private copy
If the child needs its own editable state that starts from the parent's value, copy the value into a private field. Plain fields are reactive, so the template still updates.
import { LightningElement, api } from 'lwc';
export default class InfoCard extends LightningElement {
@api heading;
_expanded = false;
isOpen = false;
@api
get expanded() {
return this._expanded;
}
set expanded(value) {
this._expanded = value;
this.isOpen = value;
}
toggle() {
this.isOpen = !this.isOpen;
}
}The parent's expanded value sets the starting state. After that, the child changes only isOpen, which it owns. The template reads isOpen.
Option 2: Dispatch an event
If the parent should know about the change, the child sends an event and the parent updates its own field. The parent then passes the new value back down.
import { LightningElement, api } from 'lwc';
export default class InfoCard extends LightningElement {
@api heading;
@api expanded = false;
handleClick() {
this.dispatchEvent(
new CustomEvent('togglerequest', {
detail: { expanded: !this.expanded }
})
);
}
}The parent listens for the event and changes the field it owns.
import { LightningElement } from 'lwc';
export default class ReleasePage extends LightningElement {
expanded = false;
handleToggleRequest(event) {
this.expanded = event.detail.expanded;
}
}<template>
<c-info-card
heading="Release notes"
expanded={expanded}
ontogglerequest={handleToggleRequest}
></c-info-card>
</template>When expanded changes in the parent, the child receives the new value and rerenders. One component owns the state, and every other component reads it.
Choosing between the two options
| Situation | Use |
|---|---|
| The child only needs a starting value and then manages its own state | Private copy |
| The parent or other components must know the current value | Event, with the parent owning the field |
Common mistakes
- Writing
this.someApiProp = ...in a click handler. - Mutating an array or object received through
@api, such asthis.items.push(x). - Copying a parent's object with
@trackand assuming that makes the original editable. Copy it into a new object with spread syntax instead.
Exercise
A child component declares @api value and has a button handler that runs this.value = this.value + 1. The parent sets value={count} from its own field. Explain the problem and give one way to fix it.
Show answer
The child assigns to a property that the parent owns, which breaks one-way data flow. The child's value and the parent's count can disagree, and LWC may report an error. To fix it, dispatch a custom event from the child (for example new CustomEvent('increment')) and have the parent update count, which flows back down through value={count}. If only the child needs the changing number, copy value into a private field and increment that field instead.