Overview
This example shows how a child component (c-paginator) fires custom events that are handled by a parent component (c-event-simple) to update the page state.
Child Component: c-paginator
The paginator component dispatches simple custom events without data.JavaScript (paginator.js)
- Creates
CustomEventinstances with event names (‘previous’, ‘next’) - Uses
this.dispatchEvent()to fire events - No additional configuration needed for basic events
Template (paginator.html)
Parent Component: c-event-simple
The parent component listens for and handles the custom events.JavaScript (eventSimple.js)
Template (eventSimple.html)
- Event listeners are added using
on<eventname>syntax (e.g.,onprevious,onnext) - Event names are automatically converted to lowercase in HTML
- Handler methods update component state based on which event fired
Event Flow
- User clicks a button in the paginator component
- Click handler creates and dispatches a
CustomEvent - Parent component’s event listener catches the event
- Parent’s handler method updates the page number
- Template re-renders with new page value
Source Files
- Child:
force-app/main/default/lwc/paginator/ - Parent:
force-app/main/default/lwc/eventSimple/
