Sub topic 7
Reading event.detail in the handler
This chapter assumes you can write a basic LWC with @api properties, nest one component inside another, and read ES module JavaScript. It also assumes you have already seen a child dispatch a CustomEvent and a parent attach onproductselected in its template. Here the focus is on what the handler receives and how to read it correctly.
What the handler receives
When the parent template listens with onproductselected={handleProductSelected}, LWC calls handleProductSelected with one argument: the event object. For a CustomEvent, that object has the payload in event.detail.
// productList.js
import { LightningElement } from 'lwc';
export default class ProductList extends LightningElement {
selectedId;
handleProductSelected(event) {
this.selectedId = event.detail.productId;
}
}The handler is called with this bound to the parent component, so you can assign to fields directly. Assigning to a field that the template uses causes a re-render.
<!-- productList.html -->
<template>
<c-product-tile
product-id="p1"
name="Desk"
onproductselected={handleProductSelected}>
</c-product-tile>
<p>Selected: {selectedId}</p>
</template>The flow of data
Useful properties of the event object
The event object has several properties. Some are useful in handlers, and some are easy to misuse.
| Property | Meaning | Typical use |
|---|---|---|
| `event.detail` | The payload the child passed in `detail`. | Read values the child sent. |
| `event.type` | The event name, such as `productselected`. | One handler serving several events. |
| `event.target` | The element the parent sees as the source. For an event from a child, this is the child's host element. | Identify which child fired. |
| `event.currentTarget` | The element the listener is attached to. | Rarely needed in templates. |
| `event.stopPropagation()` | Stops the event from reaching later listeners. | Keep an event from continuing upward. |
Retargeting matters here. Because of shadow DOM, a listener in the parent sees event.target as the child's host element, not an element inside the child's template. Do not rely on reaching into the child's internals through event.target.
Destructuring and defensive reads
You can destructure the detail to keep handlers short.
handleProductSelected(event) {
const { productId } = event.detail;
this.selectedId = productId;
}If a child might dispatch the event without a detail, the value is null, and event.detail.productId throws a TypeError. A child you own can guarantee the shape. If the shape is not guaranteed, guard the read.
handleProductSelected(event) {
const productId = event.detail?.productId;
if (productId) {
this.selectedId = productId;
}
}Common errors
- Reading the payload from the wrong place.
event.productIdisundefined. The data lives underevent.detail, and only if the child put it there. - Handler name mismatch. The attribute
onproductselectedmust match the dispatched event name exactly, in lowercase. A child that dispatchesproductSelectedwill not trigger it. - Mutating the payload. Objects in
detailare shared by reference with the child. If the parent changesevent.detail.item, it may alter the child's data. Copy first, for example with{ ...event.detail.item }, or have the child send a copy. - Using
thisin an unbound callback. A method named in the template is bound for you. An arrow function passed toaddEventListenerkeepsthis, but a plainfunctiondoes not. - Expecting a return value. The handler's return value is ignored. Events do not send a reply to the child.
Putting it together
A handler often does more than store one value. This version reads the payload, ignores repeat clicks, and keeps a history of selections.
// productList.js
import { LightningElement } from 'lwc';
export default class ProductList extends LightningElement {
selectedId;
history = [];
handleProductSelected(event) {
const { productId } = event.detail;
if (productId === this.selectedId) {
return;
}
this.selectedId = productId;
this.history = [...this.history, productId];
}
}Reassigning history with a new array, rather than calling push, keeps the update easy to follow and consistent with how you would treat any tracked value.
Exercise
A child dispatches new CustomEvent('productselected', { detail: { productId: 'p1' } }). The parent handler logs event.productId and gets undefined. What is wrong, and what is the fix?
Show answer
The payload is stored under event.detail, not on the event itself. Change the read to event.detail.productId.
Exercise
In the parent's handler, what does event.target refer to when the event came from c-product-tile, and why is it not the button inside the tile?
Show answer
It refers to the c-product-tile host element. Shadow DOM retargets the event so the parent sees the child's host element, not elements inside the child's template.