G-8E0CBM58L7

Events in LWC – Complete Guide for Salesforce Developers

  • Home
  • / Events in LWC – Complete Guide for Salesforce Developers
events in lwc

Lightning Web Components (LWC) is one of the most powerful frameworks in Salesforce for building modern, fast, and reusable web components. One of the most important concepts in LWC development is events. Events in LWC help different components communicate with each other, making applications more interactive and dynamic.

In this article, we’ll cover everything you need to know about events in LWC, including their types, lifecycle, and examples.

What are Events in LWC?

In simple terms, events in LWC are signals or messages that allow components to communicate. For example, when a user clicks a button in a child component, it can trigger an event that sends data to the parent component.

Just like in standard JavaScript, LWC also supports event handling, but it is optimized for Salesforce’s component-based architecture.

Types of Events in LWC

Events in LWC are broadly categorized into two main types:

1. Standard DOM Events

These are the same events you see in vanilla JavaScript, such as:

  • onclick

  • onchange

  • onblur

  • onkeyup

Example:

<template>
<lightning-button label="Click Me" onclick={handleClick}></lightning-button>
</template>

import { LightningElement } from 'lwc';

export default class EventExample extends LightningElement {
handleClick() {
alert(‘Button clicked!’);
}
}

2. Custom Events in LWC

When components need to pass data between each other, custom events are used.

How to Create a Custom Event in LWC:

  • Step 1: Define the event in the child component.

  • Step 2: Dispatch the event using CustomEvent.

  • Step 3: Listen to the event in the parent component.

Example: Child Component (childComp.js)

import { LightningElement } from 'lwc';

export default class ChildComp extends LightningElement {
sendData() {
const myEvent = new CustomEvent(‘myevent’, {
detail: { message: ‘Hello from Child Component’ }
});
this.dispatchEvent(myEvent);
}
}

Child Template (childComp.html)

<template>
<lightning-button label="Send Data" onclick={sendData}></lightning-button>
</template>

Parent Component (parentComp.html)

<template>
<c-child-comp onmyevent={handleData}></c-child-comp>
<p>{childMessage}</p>
</template>

Parent JS (parentComp.js)

import { LightningElement } from 'lwc';

export default class ParentComp extends LightningElement {
childMessage;

handleData(event) {
this.childMessage = event.detail.message;
}
}

Here, the child component dispatches an event and the parent listens for it.

Event Bubbling and Composed Events in LWC

When dispatching custom events, two key properties control their behavior:

  1. bubbles: true – The event travels up through the DOM hierarchy.

  2. composed: true – The event can pass across Shadow DOM boundaries.

Example:

const myEvent = new CustomEvent('myevent', {
detail: { message: 'Data with Bubbling' },
bubbles: true,
composed: true
});
this.dispatchEvent(myEvent);

This ensures that the event can reach not just the parent but even higher-level components.

Real-Life Use Cases of Events in LWC

  1. Form Submission – A child component sends form data to the parent.

  2. Cart Functionality – Adding an item from a product component to a cart component.

  3. Search Filters – Passing filter values from filter component to a list component.

Best Practices for Handling Events in LWC

  • Always use meaningful event names.

  • Use detail property to pass structured data.

  • Keep event handling logic in the parent component when possible.

  • Use bubbles and composed carefully to avoid unnecessary propagation.

FAQs on Events in LWC

Q1. What is the difference between standard events and custom events in LWC?
Standard events are built-in browser events like onclick, while custom events are developer-defined for component communication.

Q2. Can we pass data using events in LWC?
Yes, data can be passed using the detail property of CustomEvent.

Q3. How do I stop event propagation in LWC?
Use event.stopPropagation() to prevent further propagation.

Q4. What are bubbles and composed in LWC events?
bubbles allows events to travel up the DOM, and composed lets them cross Shadow DOM boundaries.

You may like : Platform Event Trap

Conclusion

Events in LWC play a crucial role in component communication. Whether it’s handling a button click or passing complex data between child and parent components, mastering events ensures you build efficient, reusable, and interactive Salesforce applications.

If you’re preparing for a Salesforce LWC interview, understanding custom events, bubbling, and event propagation will give you a strong edge.

Write your comment Here