Sub topic 4
Naming your event
This chapter assumes you can write a basic LWC with @api properties, nest one component inside another, and dispatch a CustomEvent as shown in the previous chapter. Here you will learn how to pick an event name that the framework accepts and that the parent can listen for.
Why the name matters
The first argument to new CustomEvent() is the event type. The parent uses this string to build the handler attribute in its template, so a name that breaks the rules fails in one of two ways. The framework warns you in the console, or the handler never runs.
The parent attaches a listener by adding on in front of the event name:
<c-product-tile onproductselected={handleProductSelected}></c-product-tile>The attribute onproductselected is the on prefix plus the event name productselected. If the child dispatches a different name, nothing calls handleProductSelected, and the browser raises no error.
The naming rules
An LWC event name must follow these rules:
- It starts with a lowercase letter.
- It contains only lowercase letters, numbers, and underscores.
- It has no uppercase letters, spaces, or hyphens.
- It does not start with
on. The template adds that prefix itself, so a name likeonselectwould produce the attributeononselect.
HTML attribute names are not case-sensitive, so the template compiler cannot tell productSelected from productselected. Requiring lowercase removes that ambiguity.
| Name | Valid | Reason |
|---|---|---|
| `productselected` | Yes | Lowercase letters only |
| `product_selected` | Yes | Underscore separates words |
| `select2` | Yes | Digits are allowed after the first letter |
| `productSelected` | No | Contains an uppercase letter |
| `product-selected` | No | Contains a hyphen |
| `product selected` | No | Contains a space |
| `onselect` | No | Starts with `on` |
| `2select` | No | Starts with a digit |
Putting the name together
The name appears in two places that must match exactly. The child dispatches it:
// productTile.js
import { LightningElement, api } from 'lwc';
export default class ProductTile extends LightningElement {
@api productId;
handleClick() {
this.dispatchEvent(
new CustomEvent('product_selected', {
detail: { productId: this.productId }
})
);
}
}The parent listens with on plus the same string:
<!-- productList.html -->
<template>
<c-product-tile
product-id="p1"
onproduct_selected={handleProductSelected}>
</c-product-tile>
</template>Both names use an underscore, so the attribute reads onproduct_selected. Many teams skip separators and write productselected so the attribute stays easy to read. Pick one style and apply it across your codebase.
Choosing a good name
The rules decide what is allowed. These habits decide what is clear:
- Describe what happened, not what the parent should do.
product_selectedstates a fact.show_detailstells the parent how to react, which couples the child to one parent's behavior. - Use a verb in the past tense or a noun phrase for a change, such as
closed,saved, orfilterchange. Readers can tell at a glance that it is an event. - Avoid names that collide with native DOM events.
click,change,focus, andselectalready exist in the browser. If you dispatch your ownchangeevent, handlers for the native one and yours can fire together. Use a more specific name such asquantitychange. - Keep names stable. Renaming an event silently breaks every parent that listens for the old one.
Common mistakes
- Dispatching
productSelectedand then writingonproductSelectedin the template. The template attribute is lowercased, so it never matches. - Writing
onproduct-selectedfor an event namedproduct_selected. Hyphens and underscores are different characters. - Dispatching
new CustomEvent('onselected'). The parent would needononselected. - Misspelling the name in only one file. A mismatch produces no error, so compare the child's string with the parent's attribute character by character when a handler does not fire.
Exercise
A child component dispatches new CustomEvent('itemAdded'). List two problems with this name and give a corrected name with the matching handler attribute for a parent template.
Show answer
The name contains an uppercase letter, which LWC event names do not allow, and the lowercased template attribute onitemadded would not match itemAdded anyway. A valid name is itemadded (or item_added), and the parent listens with onitemadded={handler} (or onitem_added={handler}).
Exercise
Why is change a poor name for a custom event on a component that wraps an input?
Show answer
change is also a native DOM event name. Handlers can receive both the native event and your custom one, which makes the behavior confusing. A more specific name such as quantitychange avoids the collision.