Sub topic 6
Listening in the parent template
This chapter assumes you can write a basic LWC with an HTML template and a JavaScript class. It also assumes you know how to expose a property with @api and how to place one component inside another. The previous chapters showed how a child creates and dispatches a CustomEvent. Here you wire up the parent so it receives that event.
The `on` prefix convention
A parent listens for a child's event by adding an attribute to the child's tag in its template. The attribute name is on followed by the event name. The attribute value is a handler method from the parent's class, in curly braces.
For a child that dispatches productselected, the attribute is onproductselected. For an event named save, it would be onsave.
<!-- productList.html -->
<template>
<c-product-tile
product-id="p1"
name="Desk"
onproductselected={handleProductSelected}>
</c-product-tile>
<p>Selected: {selectedId}</p>
</template>Three details matter here:
- The attribute name is all lowercase. It must match the dispatched event name exactly, with
onin front. - The value is
{handleProductSelected}, with no parentheses and no quotes. You pass a reference to the method and do not call it. - The handler must be a method on the parent's class. LWC templates cannot contain inline expressions or arrow functions.
The child side, for reference
The parent can only hear what the child sends. This is the child from the earlier chapters.
// 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 }
})
);
}
}<!-- productTile.html -->
<template>
<button onclick={handleClick}>{name}</button>
</template>The onclick in the child template is a native browser event on a button. The onproductselected in the parent template is your own custom event on a component. Both use the same on syntax.
Writing the handler
The handler is a normal class method. LWC calls it with one argument, the event object. The data the child attached is in event.detail.
// productList.js
import { LightningElement } from 'lwc';
export default class ProductList extends LightningElement {
selectedId;
handleProductSelected(event) {
this.selectedId = event.detail.productId;
}
}Fields in an LWC class are reactive, so assigning this.selectedId re-renders the template. After the click, the paragraph shows Selected: p1.
What happens at runtime
The diagram shows the order of events from click to re-render.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Handler never runs | Attribute does not match the event name, for example `onProductSelected` for an event named `productselected` | Use lowercase and match the name exactly after `on` |
| Handler never runs | Event name contains uppercase letters, hyphens, or spaces, so it cannot be matched by an attribute | Rename the event to lowercase letters, numbers, and underscores only |
| `event.detail` is `undefined` | The child dispatched the event without a `detail` option | Add `detail` in the `CustomEvent` constructor |
| Error reading a property of `event.detail` | The child sent a primitive or a different shape than the parent expects | Agree on the payload shape and read it consistently |
| Handler runs on the wrong object | The method uses `this` after being passed somewhere else | Keep the handler as a class method and reference it directly in the template |
Exercises
Exercise
A child dispatches new CustomEvent('itemremoved', { detail: { id: 7 } }). Write the attribute you add to <c-item-row> in the parent template, and the line inside the handler that reads the id.
Show answer
The attribute is onitemremoved={handleItemRemoved}. Inside the handler, which receives event, read the id with event.detail.id.
Exercise
A parent template has <c-product-tile onproductselected={handleProductSelected()}>. The handler either runs at the wrong time or the component fails to compile. What is wrong and how do you fix it?
Show answer
The template passes a call expression, with parentheses, instead of a reference to the method. Use onproductselected={handleProductSelected} so LWC calls the method when the event fires.