Sub topic 2
Creating a CustomEvent
This chapter covers how a child component builds a CustomEvent. Dispatching it, naming rules, and parent handlers each get their own chapter, so this one stays on construction.
It assumes you know basic JavaScript (classes, objects, ES modules), the structure of an LWC component (an HTML template plus a JavaScript class), how @api exposes public properties, and how to place one component inside another's template.
Why a CustomEvent
In LWC, data moves down through public properties and signals move up through events. A parent sets @api properties on a child. A child cannot reach into its parent, so it announces that something happened and lets the parent decide what to do.
CustomEvent is a standard browser class, not an LWC invention. LWC uses it as is. You create an instance, describe it with a name and a small options object, and later hand it to the component's dispatchEvent method.
The constructor
The constructor takes two arguments.
new CustomEvent(type, options);typeis a string, the event name. Parents listen for this name.optionsis an optional object. Three keys matter in LWC:detail,bubbles, andcomposed.
| Option | Default | Purpose |
|---|---|---|
| `detail` | `null` | Data you want to send with the event. |
| `bubbles` | `false` | Whether the event travels up through ancestor elements in the same template. |
| `composed` | `false` | Whether the event can cross a shadow boundary. |
For most child-to-parent communication you need only the name and, often, detail. Leave bubbles and composed at their defaults until you have a reason to change them. A later chapter covers them.
A minimal event
The simplest event carries no data. It only says "this happened".
const closeEvent = new CustomEvent('close');Creating the object does nothing visible. No listener runs until the component dispatches it with this.dispatchEvent(closeEvent). Construction and dispatch are separate steps, which is why this chapter stops at the first.
An event with a payload
When the parent needs information, put it in detail. Here is the event from the product tile example.
// productTile.js
import { LightningElement, api } from 'lwc';
export default class ProductTile extends LightningElement {
@api productId;
@api name;
handleClick() {
const selectEvent = new CustomEvent('productselected', {
detail: { productId: this.productId }
});
// Dispatching is covered in the next chapter.
this.dispatchEvent(selectEvent);
}
}The event name is productselected. The detail value is an object with one key, productId. The parent will later read it as event.detail.productId.
A few points about this code:
- The event is created inside a method, not in the constructor or at module level. Build it at the moment the thing happens, so
detailholds current values. detailcan be any value, but an object with named keys is easier to extend. Adding a second key later does not break existing parents.- A new event object is created each time. Do not reuse one instance for repeated dispatches.
Keep the payload safe
The parent receives whatever you put in detail. Two habits prevent subtle bugs:
- Send plain data such as strings, numbers, booleans, and small objects. Avoid passing component instances or DOM elements.
- Do not hand over an object that the child keeps changing. The parent and child would share a reference, and a later change in one would surprise the other. Copy it first, for example
{ ...this.filters }.
const filterEvent = new CustomEvent('filterchange', {
detail: { ...this.filters }
});Values passed down through @api properties are read-only inside the child, and that protection does not apply here. Data you send up in detail is your responsibility.
Common mistakes
- Calling
new CustomEventand never dispatching it. Nothing happens, and no error appears. - Putting data directly on the options object, such as
{ productId: 'p1' }. Onlydetailcarries a payload, so the parent would seeevent.detailasnull. - Setting
bubbles: trueandcomposed: trueby default "to be safe". This exposes the event to components that should not see it.
Exercise
A child component builds new CustomEvent('save', { id: 42 }) and dispatches it. The parent handler reads event.detail.id and gets an error. What is wrong, and how do you fix it?
Show answer
The payload is on the options object instead of under detail, so event.detail is null and reading .id throws. Fix it by writing new CustomEvent('save', { detail: { id: 42 } }).
Exercise
Why should you build the CustomEvent inside the handler method instead of once at the top of the file and reuse it?
Show answer
The event's detail should reflect the values at the moment the action happens. A single shared instance would hold stale values, and each dispatch should use a fresh event object.