Overview
Custom events can carry data from child to parent using the detail property of CustomEvent. This enables rich communication patterns where child components share complex information upward.
Passing Data with Events
Use the detail property in the CustomEvent constructor to pass data to the parent component.
contactListItem.js
contactListItem.html
Accessing Event Data
In the parent component, access the data through event.detail.
Parent Component (c-event-with-data)
eventWithData.js
eventWithData.html
What Can You Pass?
The detail property can contain:
- Primitives: strings, numbers, booleans
- Objects: plain JavaScript objects
- Arrays: lists of data
- Complex data: nested objects and arrays
Do NOT pass DOM elements, functions, or symbols in event detail. Events can cross shadow DOM boundaries, and these types cannot be safely transmitted.
Common Patterns
Passing a Single Value
Passing Multiple Values
Passing Complex Objects
Best Practices
- Keep detail simple: Only pass what the parent needs
- Use meaningful property names: Make the detail object self-documenting
- Avoid passing entire objects unnecessarily: Pass IDs when possible
- Document the detail structure: Clearly specify what data the event carries
- Validate data before dispatching: Ensure the detail contains valid data
Source Code
View the complete source:
- Child:
force-app/main/default/lwc/contactListItem/
- Parent:
force-app/main/default/lwc/eventWithData/