Sub topic 13
Returning data to the parent with custom events
This chapter assumes you can build a parent and child component pair, mark child fields with @api, and bind values in a template. It also assumes you know the one-way data flow rule from earlier chapters.
Why the child needs another channel
Public properties carry data down from parent to child. A child must not change the values it receives, so it needs a different way to report something back, such as "the user clicked Complete". That channel is a custom event.
The child announces what happened. The parent decides what to do about it. The child never reaches into the parent or edits the parent's data.
Dispatching an event from the child
The child creates a CustomEvent and sends it with this.dispatchEvent(). The first argument is the event name. The optional second argument can include a detail property, which holds the payload.
// taskCard.js (child)
import { LightningElement, api } from 'lwc';
export default class TaskCard extends LightningElement {
@api taskId;
@api heading;
handleComplete() {
this.dispatchEvent(
new CustomEvent('complete', {
detail: { taskId: this.taskId }
})
);
}
}<!-- taskCard.html (child) -->
<template>
<article>
<h3>{heading}</h3>
<button onclick={handleComplete}>Complete</button>
</article>
</template>The payload holds only the task's ID, not the whole task. A small payload keeps the child from handing out its data for others to modify.
Listening in the parent
In the parent's template, add an on prefix to the event name and bind it to a handler method. The event complete becomes the attribute oncomplete.
<!-- taskBoard.html (parent) -->
<template>
<template for:each={tasks} for:item="task">
<c-task-card
key={task.id}
task-id={task.id}
heading={task.name}
oncomplete={handleComplete}>
</c-task-card>
</template>
</template>The handler receives the event object. Read the payload from event.detail.
// taskBoard.js (parent)
import { LightningElement } from 'lwc';
export default class TaskBoard extends LightningElement {
tasks = [
{ id: 't1', name: 'Write documentation' },
{ id: 't2', name: 'Review pull request' }
];
handleComplete(event) {
const { taskId } = event.detail;
this.tasks = this.tasks.filter((task) => task.id !== taskId);
}
}The handler assigns a new array to tasks. That assignment makes the parent re-render, and the list of children updates to match. The whole loop is: data goes down through properties, a message goes up through an event, the parent changes its own data, and new values go down again.
Event options
By default, an event reaches only the parent that directly contains the child. It does not bubble up further. The bubbles and composed options change this, but you rarely need them for parent and child communication. Use the defaults first.
| Option | Default | Effect when `true` |
|---|---|---|
| `bubbles` | `false` | The event travels up through ancestor elements |
| `composed` | `false` | The event can cross shadow DOM boundaries |
Common mistakes
- Putting uppercase letters or hyphens in the event name. The parent's
onattribute then fails to match. - Putting a whole object from the child's state in
detail. The parent could then modify the child's data. Send an ID or a copy. - Changing the parent's data from the child. Dispatch an event instead and let the parent update it.
- Forgetting the
onprefix in the parent markup.complete={handleComplete}sets a property and listens for nothing.
Exercise
A child dispatches new CustomEvent('itemselected', { detail: { id: 7 } }). What attribute does the parent put on the <c-item-list> tag to handle it, and how does the handler read the ID?
Show answer
The parent writes onitemselected={handleSelected} on the tag. In handleSelected(event), it reads the ID with event.detail.id.