Sub topic 8
Passing objects and arrays, and copying them in the child
This chapter assumes you can build a basic Lightning web component and know how @api exposes a public property. It also assumes you know that a child must not change values it receives from its parent. Here you will apply that rule to objects and arrays.
Why objects and arrays need extra care
A string or number is copied when the parent passes it. An object or array is passed as a reference, so the child sees the parent's actual data. Lightning Web Components make values received from a parent read-only. The child cannot assign to a received property, and it cannot change the members of a received object or array.
That means the child cannot call push, sort or splice on an array it received, and it cannot assign value.name = 'x'. The attempt fails, and the error message depends on the operation. For example, sort on a read-only array throws a TypeError.
The fix is to copy the data into a field the child owns, then change the copy.
Copying with the spread operator
Spread syntax makes a shallow copy. For an array, write [...value]. For an object, write { ...value }.
// taskList.js (child)
import { LightningElement, api } from 'lwc';
export default class TaskList extends LightningElement {
_tasks = [];
@api
get tasks() {
return this._tasks;
}
set tasks(value) {
// Copy the incoming array: values from a parent are read-only in the child.
this._tasks = Array.isArray(value) ? [...value] : [];
}
handleSortByName() {
this._tasks = [...this._tasks].sort((a, b) => a.name.localeCompare(b.name));
}
}The setter does two jobs. It checks that the value is an array, and it stores a copy. Because _tasks belongs to the child, handleSortByName can replace it with a sorted copy. The parent's array stays in its original order.
The sort line copies again before sorting. sort changes the array in place, so sorting a copy and assigning the result keeps the pattern consistent and avoids surprises.
How the data moves
Shallow copies and nested objects
Spread copies only the top level. If each task is an object, the copied array holds the same task objects the parent holds. Those task objects are still read-only in the child, so this._tasks[0].name = 'x' still fails.
To change a nested object, copy that object too.
// Inside the child class
handleRename(event) {
const id = event.target.dataset.id;
const newName = event.target.value;
this._tasks = this._tasks.map((task) =>
task.id === id ? { ...task, name: newName } : task
);
}map builds a new array. Only the matching task is replaced by a copy with a new name. Every other task is reused as is.
Updates from the parent
If the parent later gives the child a new array, the setter runs again and the child stores a fresh copy. Any sorting the child did to its previous copy is lost. If the child should keep its own state across parent updates, keep that state in a separate field, such as a sort direction, and apply it inside the setter.
Common mistakes
- Assigning to a received object's field. The read-only restriction blocks it.
- Calling
push,sortorsplicedirectly on a received array. - Copying an array but then editing the nested objects.
- Skipping the
Array.isArraycheck, so anullvalue breaks later code.
Exercise
A child receives @api items as an array of objects such as { id: 1, label: 'A' }. A button handler runs this.items[0].label = 'B'. Why does it fail, and how would you rename the first item's label without touching the parent's data?
Show answer
Values received from a parent are read-only, and that includes the objects inside an array. Use a setter to store a copy in a private field, then replace the item with a modified copy: this._items = this._items.map((item, i) => i === 0 ? { ...item, label: 'B' } : item);