LearnFrenzy
Sign in Take a free test

Component Communication · Lesson 9 of 25

Custom Events in LWC: Child to Parent Communication

6 min read Updated Nov 8, 2026 Saurabh Samir

Data flows down through @api properties; information flows up through events. When something happens in a child – a row is selected, a filter changes, a form is saved – the child dispatches a standard DOM CustomEvent, and the parent listens for it. This "data down, events up" pattern keeps every component reusable: the child doesn't need to know who's listening.

In this lesson

  • Dispatching and handling a custom event
  • Sending data with detail
  • Event naming rules
  • bubbles and composed

Dispatch and listen

<!-- productTile.html -->
<template>
    <div class="slds-box">
        <p>{name} – ${price}</p>
        <lightning-button label={buttonLabel} onclick={handleAdd}></lightning-button>
    </div>
</template>
// productTile.js
import { LightningElement, api } from 'lwc';

export default class ProductTile extends LightningElement {
    @api productId;
    @api name;
    @api price;

    get buttonLabel() {
        return `Add ${this.name}`;
    }

    handleAdd() {
        // tell whoever is listening; send only what they need
        this.dispatchEvent(new CustomEvent('addtocart', {
            detail: { productId: this.productId, price: this.price }
        }));
    }
}
<!-- shopPage.html -->
<template>
    <p>Items in cart: {cartCount} · Total: ${cartTotal}</p>
    <c-product-tile product-id="P1" name="Keyboard" price="40" onaddtocart={handleAddToCart}></c-product-tile>
    <c-product-tile product-id="P2" name="Monitor" price="180" onaddtocart={handleAddToCart}></c-product-tile>
</template>
// shopPage.js
import { LightningElement } from 'lwc';

export default class ShopPage extends LightningElement {
    cart = [];

    get cartCount() {
        return this.cart.length;
    }

    get cartTotal() {
        return this.cart.reduce((sum, item) => sum + Number(item.price), 0);
    }

    handleAddToCart(event) {
        const { productId, price } = event.detail;
        this.cart = [...this.cart, { productId, price }];
    }
}

Result

Items in cart: 0 · Total: $0
Keyboard – $40
[Add Keyboard]
Monitor – $180
[Add Monitor]

› click “Add Keyboard”
Items in cart: 1 · Total: $40
Keyboard – $40
[Add Keyboard]
Monitor – $180
[Add Monitor]

› click “Add Monitor”
Items in cart: 2 · Total: $220
Keyboard – $40
[Add Keyboard]
Monitor – $180
[Add Monitor]

› click “Add Keyboard”
Items in cart: 3 · Total: $260
Keyboard – $40
[Add Keyboard]
Monitor – $180
[Add Monitor]

Data down, events up

  1. DownshopPage passes name and price to each productTile
  2. Upa click makes the tile dispatch addtocart with a detail payload
  3. HandleshopPage's onaddtocart handler updates its cart field
  4. Down againthe new total renders; the tiles never touched the cart

Naming rules

  • Event names are lowercase, with no hyphens or spaces: addtocart, rowselect. Prefixing with "on" is not allowed in the name.
  • The listener attribute is on + the name: onaddtocart={handler}.
  • Put data in detail, and send primitives or new plain objects – not internal state the parent could mutate.

Listening in JavaScript

You can also add a listener in code, for example on the component itself:

connectedCallback() {
    this.template.addEventListener('notify', this.handleNotify);   // events from elements in this template
}

Declarative onxyz attributes are usually simpler, and LWC removes them automatically.

bubbles and composed

By default a custom event is delivered only to the element that dispatched it – the child's host element, where the parent's onxyz listener sits. Two options widen that:

OptionsWho can hear itUse
{ } (default)the parent that placed the childalmost always – the recommended default
{ bubbles: true }ancestors inside the same templateone handler on a container for many children (event delegation)
{ bubbles: true, composed: true }ancestors across shadow boundaries, up to the documentrarely – it exposes the event to the whole page and becomes part of your public API
<!-- ratingStar.html -->
<template>
    <lightning-button label={label} onclick={handleClick}></lightning-button>
</template>
// ratingStar.js
import { LightningElement, api } from 'lwc';

export default class RatingStar extends LightningElement {
    @api value;
    get label() {
        return `${this.value}★`;
    }
    handleClick() {
        this.dispatchEvent(new CustomEvent('rate', { detail: Number(this.value), bubbles: true }));
    }
}
<!-- ratingInput.html -->
<template>
    <div onrate={handleRate}>                       <!-- one listener for all five children -->
        <template for:each={stars} for:item="s">
            <c-rating-star key={s} value={s}></c-rating-star>
        </template>
    </div>
    <p>Your rating: {rating}</p>
</template>
// ratingInput.js
import { LightningElement } from 'lwc';

export default class RatingInput extends LightningElement {
    stars = [1, 2, 3, 4, 5];
    rating = 'not rated';

    handleRate(event) {
        this.rating = `${event.detail}/5`;
        this.dispatchEvent(new CustomEvent('change', { detail: { rating: event.detail } }));   // tell our own parent
    }
}

Result and events received by the parent

[1★] [2★] [3★] [4★] [5★]
Your rating: not rated

› click “4★”
[1★] [2★] [3★] [4★] [5★]
Your rating: 4/5
event "change" detail: {"rating":4}

› click “5★”
[1★] [2★] [3★] [4★] [5★]
Your rating: 5/5
event "change" detail: {"rating":5}

bubbles: true let one onrate handler on the wrapping div catch events from all five stars. Then ratingInput fires its own change event for its parent – each component exposes its own clear events rather than leaking its children's.

Avoid bubbles + composed by default

A composed event escapes your component and becomes visible to every ancestor, so its name and payload become a public contract you can't easily change. For communication between components that aren't parent and child, use Lightning Message Service (next lesson).

Practice

Try it yourself

  1. Make a searchBox that dispatches a search event with the typed term when the user clicks "Search", and a parent that shows "Searching for: …".

    Show solution
    <!-- searchBox.html -->
    <template>
        <lightning-input label="Search accounts" value={term} onchange={handleChange}></lightning-input>
        <lightning-button label="Search" onclick={handleSearch}></lightning-button>
    </template>
    // searchBox.js
    import { LightningElement } from 'lwc';
    
    export default class SearchBox extends LightningElement {
        term = '';
        handleChange(event) {
            this.term = event.detail.value;
        }
        handleSearch() {
            this.dispatchEvent(new CustomEvent('search', { detail: this.term.trim() }));
        }
    }
    <!-- accountFinder.html -->
    <template>
        <c-search-box onsearch={handleSearch}></c-search-box>
        <p>Searching for: {query}</p>
    </template>
    // accountFinder.js
    import { LightningElement } from 'lwc';
    
    export default class AccountFinder extends LightningElement {
        query = '–';
        handleSearch(event) {
            this.query = event.detail;
        }
    }

    Result

    Search accounts [] [Search]
    Searching for: –
    
    › type “Acme” into Search accounts
    Search accounts [Acme] [Search]
    Searching for: –
    
    › click “Search”
    Search accounts [Acme] [Search]
    Searching for: Acme

Quick check

Is add-to-cart a valid event name?

No – use lowercase without hyphens: addtocart.

Where does the parent read the event's data?

From event.detail.

What do bubbles and composed do?

bubbles lets ancestors in the same template hear it; composed also crosses shadow boundaries.

Summary

  • Children dispatch CustomEvent with data in detail; parents listen with on + name.
  • Event names are lowercase with no hyphens.
  • Keep events non-bubbling by default; use bubbles for delegation and avoid composed unless you mean it.

Finished this lesson?

Mark it complete to track your progress, then carry on with the next lesson.

Next lesson

Search LearnFrenzy

Esc

Quick links