
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:
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)
Child Template (childComp.html)
Parent Component (parentComp.html)
Parent JS (parentComp.js)
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:
-
bubbles: true – The event travels up through the DOM hierarchy.
-
composed: true – The event can pass across Shadow DOM boundaries.
Example:
This ensures that the event can reach not just the parent but even higher-level components.
Real-Life Use Cases of Events in LWC
-
Form Submission – A child component sends form data to the parent.
-
Cart Functionality – Adding an item from a product component to a cart component.
-
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.