Sub topic 12
Communicating upward with CustomEvent
This chapter assumes you know how @api public properties pass data down from a parent to a child, and that you have used addEventListener in the browser. Here you will learn the other direction: how a child component tells its parent that something happened.
The rule: properties down, events up
A parent sets public properties on a child. A child never reaches into its parent. Instead, the child dispatches an event, and the parent decides what to do with it. This keeps the child reusable, because it does not need to know who is listening.
LWC uses the standard DOM CustomEvent for this. There is no LWC-specific event class.
Dispatching an event from the child
The child below keeps a count. On each click it increments the count and dispatches an event named countchange.
// counterButton.js
import { LightningElement, api } from 'lwc';
export default class CounterButton extends LightningElement {
@api label = 'Add';
count = 0;
handleClick() {
this.count += 1;
this.dispatchEvent(
new CustomEvent('countchange', { detail: { count: this.count } })
);
}
}Two parts matter here:
- The first argument is the event name. Use lowercase letters and no spaces or hyphens, for example
countchange. - The
detailproperty carries your data. Pass a plain object so the parent readsevent.detail.count.
Listening in the parent
In the parent template, you attach a handler with the on prefix followed by the event name. The event countchange becomes the attribute oncountchange.
<!-- parentPanel.html -->
<template>
<c-counter-button label="Increase" oncountchange={handleCountChange}></c-counter-button>
</template>The handler is a method on the parent class:
// parentPanel.js
import { LightningElement } from 'lwc';
export default class ParentPanel extends LightningElement {
total = 0;
handleCountChange(event) {
this.total = event.detail.count;
}
}Because total is a class field, reassigning it re-renders the parent's template.
Bubbling and composition
By default, a CustomEvent does not bubble and is not composed. Only a listener on the element that dispatched it, which is the child's host element, will receive it. This is why oncountchange on the <c-counter-button> tag works.
| Option | Default | Effect when `true` |
|---|---|---|
| `bubbles` | `false` | The event travels up through ancestors in the same DOM tree. |
| `composed` | `false` | The event also crosses shadow DOM boundaries. |
Common errors
- Uppercase or hyphenated event names. The template attribute
oncountChangedoes not matchcountchange. Keep names lowercase. - Mutating
detaildata later. Treat thedetailobject as read-only after dispatch. - Listening on the wrong element. Without
bubbles, a listener on a grandparent never fires. - Forgetting that the handler runs with the parent's
this. Declare handlers as class methods, so the template binds them correctly.
Exercise
A child component dispatches new CustomEvent('itemselect', { detail: { id: 7 } }). Write the attribute you place on the child's tag in the parent template, and say how the handler reads the id.
Show answer
Add onitemselect={handleItemSelect} to the child's tag. In the handler, read event.detail.id.
Exercise
A grandparent listens for an event that a grandchild dispatches, but the handler never runs. The event was created with no options. Why, and what would you change?
Show answer
The default bubbles: false keeps the event on the grandchild's host element, so ancestors never see it. Either have the intermediate parent listen and re-dispatch its own event, or set bubbles: true (plus composed: true if it must cross shadow boundaries). Re-dispatching keeps the public API smaller.