# How do child-to-parent communication channels work in LWC using Custom Events?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Communication, Custom Events

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-child-to-parent-communication-channels-work-in-lwc-using-custom-events

## Answer

**Basics:** A child should not reach into its parent. Instead it announces that something happened ("item selected") with data, and whoever contains it decides what to do. This keeps components reusable.

**How it works:**
1. In the child, create `new CustomEvent('itemselected', { detail: { id } })` and call `this.dispatchEvent(evt)`.
2. In the parent template, attach `onitemselected={handleSelect}` to the child tag.
3. The handler receives the event; read `event.detail`.
4. Options: `bubbles` (travels up the DOM tree), `composed` (crosses shadow boundaries). Both default to false, so a plain event is seen only by a listener on the child element itself.
5. Imperative listening is possible with `addEventListener` on `this.template` or on an element reference.

**Advanced / deep dive:** Event names should be lowercase, no spaces, and avoid uppercase letters and hyphens; underscores are acceptable; do not prefix with `on`. Choose the narrowest propagation: non-bubbling for direct parent, bubbling without composed for ancestors within one shadow tree, and both only when a distant container must hear it, because composed bubbling leaks across component boundaries and complicates reuse. `detail` should carry immutable, serializable data; a parent mutating a shared object would affect the child. For sibling or unrelated components use Lightning Message Service or a pub/sub module instead of bubbling hacks. For forms, standard events such as `change` from `lightning-input` follow the same mechanism and expose `event.target.value`. A parent can also call `event.stopPropagation()` to prevent further travel. Events are synchronous, so handlers run in the dispatch call.

**Code / config example:**
```javascript
// child.js
import { LightningElement, api } from 'lwc';
export default class ProductTile extends LightningElement {
    @api productId;
    handleClick() {
        this.dispatchEvent(new CustomEvent('select', {
            detail: { productId: this.productId }   // plain data only
        }));
    }
}
```
```html
<!-- parent.html -->
<c-product-tile product-id={p.Id} onselect={handleSelect}></c-product-tile>
```
```javascript
// parent.js
handleSelect(event) { this.selectedId = event.detail.productId; }
```

**Hidden gotchas & must-remember points:**
- Default `bubbles: false, composed: false` means grandparents never hear the event.
- Event names with uppercase letters or hyphens cause handler-binding problems; keep lowercase.
- Passing complex objects in `detail` shares references; clone or freeze them.
- In the template you write `onitemselected`, not `onItemSelected`.
- Overusing `composed: true` couples components to page structure.
- Aura parents can listen to LWC events through the `on<eventname>` attribute with the lowercase name.

**Best practice:** Use narrow, well-named events carrying minimal immutable data, and use LMS for non-hierarchical communication.

---
Source: ForceChallenges · last updated 2026-11-02
