Sub topic 5
Sending data with the detail property
This chapter covers how a child component attaches data to a CustomEvent and how that data reaches the parent. It assumes you know basic JavaScript (classes, objects, modules), the structure of an LWC component, @api properties, and how to place one component inside another's template. It also assumes you can already create and dispatch a CustomEvent.
What detail is
A CustomEvent has one built-in slot for your own data: the detail property. You set it in the second argument of the constructor. Whatever you put there is available to every listener as event.detail.
// productTile.js
import { LightningElement, api } from 'lwc';
export default class ProductTile extends LightningElement {
@api productId;
@api name;
handleClick() {
this.dispatchEvent(
new CustomEvent('productselected', {
detail: { productId: this.productId }
})
);
}
}The event carries one small object with a single key, productId. If you leave detail out, event.detail is null. That is fine for a pure signal such as "the dialog was closed".
How the data travels
The child builds the event, the browser delivers it to the parent's listener, and the parent reads event.detail.
Reading the payload in the parent
The parent template attaches a handler with the on prefix plus the event name.
<!-- productList.html -->
<template>
<c-product-tile
product-id="p1"
name="Desk"
onproductselected={handleProductSelected}>
</c-product-tile>
<p>Selected: {selectedId}</p>
</template>The handler receives the event object. Read the data from event.detail.
// productList.js
import { LightningElement } from 'lwc';
export default class ProductList extends LightningElement {
selectedId;
handleProductSelected(event) {
this.selectedId = event.detail.productId;
}
}Assigning to selectedId re-renders the template, so the paragraph shows the new value.
Choosing what goes in detail
Keep the payload small and specific. Send what the parent needs to decide what to do next, and nothing more.
- Prefer an object with named keys.
{ productId: 'p1' }is easier to extend than a bare string. You can addquantitylater without changing every listener. - Send identifiers and plain values. Strings, numbers, booleans, and plain objects or arrays of them are the safe choices.
- Avoid passing the component itself or its internal state. The parent should not depend on how the child is built.
- Do not pass data you intend to keep editing. See the next section.
Mutation and shared references
The detail object is passed by reference. The parent receives the same object the child created, not a copy. If the child puts one of its own objects or arrays in detail and later changes it, the parent sees the change. If the parent changes it, the child's data changes as well.
Properties that a parent passes down to a child through @api are read-only inside the child. LWC wraps them in a read-only proxy, so the child cannot mutate them. Passing such an object straight back up in detail therefore gives the parent a wrapped value that it cannot modify.
When you want an independent copy, build one when you dispatch:
handleSave() {
this.dispatchEvent(
new CustomEvent('save', {
detail: { fields: { ...this.fields }, tags: [...this.tags] }
})
);
}The spread syntax makes a shallow copy. Nested objects inside fields are still shared. For deeper structures, copy them explicitly, or send only the values that the parent needs.
Common errors
| Symptom | Likely cause |
|---|---|
| `event.detail` is `null` | The constructor had no `detail` option, or the key was misspelled, for example `details`. |
| `Cannot read properties of null (reading 'productId')` | The parent reads a key on `detail` that the child did not send. |
| `event.detail.productId` is `undefined` | The child sent a different key name than the parent expects. |
| The parent cannot change a received object | The object came from a read-only `@api` value. Copy it first. |
Exercise
A child dispatches new CustomEvent('rated', { detail: 4 }). The parent handler needs the rating. What does it read, and why might an object such as { rating: 4 } be a better design?
Show answer
The handler reads event.detail, which is 4. An object with a named key such as { rating: 4 } is read as event.detail.rating. It is self-describing, and you can add more fields later, such as a productId, without changing how existing listeners read the value.