Sub topic 14
Common pitfalls and how to debug them
This chapter assumes you know how to expose a public property with @api, set it from a parent template, and copy objects and arrays in a child. It collects the mistakes that most often break parent-to-child data flow and shows how to find each one.
Pitfall 1: The attribute name does not match
JavaScript property names use camelCase. Markup attributes use kebab-case. If the parent writes the wrong form, the child property stays undefined and nothing reports an error.
<!-- Wrong: camelCase in markup -->
<c-task-card dueDate={due}></c-task-card>
<!-- Right: kebab-case in markup -->
<c-task-card due-date={due}></c-task-card>The child property is still declared as @api dueDate;. Check the spelling on both sides first.
Pitfall 2: A boolean that defaults to true
An attribute written in markup without a value is true. The parent also cannot turn a true default into false by writing false as a literal. The literal is a string, and a string is truthy. Declare boolean public properties with a false default.
import { LightningElement, api } from 'lwc';
export default class TaskCard extends LightningElement {
@api completed = false;
}<c-task-card completed></c-task-card>Pitfall 3: The child changes a received value
Values that arrive through @api are read-only in the child. Assigning to a received object or pushing to a received array throws an error or fails silently, depending on the value. Copy the data first and change the copy.
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] : [];
}
handleAdd() {
this._tasks = [...this._tasks, { id: 'new', name: 'New task' }];
}
}The spread operator makes a shallow copy. If you change nested objects inside the copy, copy those objects too.
Pitfall 4: Setter and getter problems
Two rules cause most setter bugs.
- Put
@apion only one accessor of a pair. Put it on the getter, and the setter is exposed with it. - The setter can run before the component is rendered, and before other properties are set. Do not read
this.templateor other public properties inside it.
Also, never store the value under the same name as the accessor. this.tasks = value inside the tasks setter calls itself forever. Use a private field such as _tasks.
Pitfall 5: Missing or duplicate keys in a loop
Each outermost element inside for:each needs a key whose value is unique and stable within that list. Do not use the array index if items can be reordered or removed. Use an ID from the data.
Pitfall 6: Mutating data and expecting a re-render
The child re-renders when a bound value changes by reference. If the parent changes a field inside an object in place, the child can miss the update. Assign a new object or array to the parent's field.
A debugging routine
Work through these steps in order.
- Open the browser developer tools and log the property in the child, for example inside a getter or in
renderedCallback. If it isundefined, the problem is the attribute name or the binding. - Compare the attribute in the parent markup with the
@apiname, letter by letter, in kebab-case. - Confirm the parent's field has a value when the template renders. Log it in the parent.
- Check the console for read-only errors. These point to a child that modifies received data.
- Check whether the parent changes data in place. If so, assign a new copy.
Exercise
A parent writes <c-task-card due-date={due}></c-task-card>, and the child declares @api dueDate;. The child shows nothing, and due has a value in the parent. Name two other things you would check.
Show answer
Check that the child template displays {dueDate} with the same spelling, and that the child does not overwrite or reset dueDate in its own code. Also check that the parent assigns due before the template renders, and that the child reads the value after it arrives (not in the constructor).
Exercise
Why does set tasks(value) { this.tasks = value; } fail, and how do you fix it?
Show answer
The setter assigns to the property that it belongs to, so it calls itself without end. Store the value in a private field such as this._tasks = value instead.