Skip to main content
Deprecated: Use callback props and/or the $host() rune instead. See the migration guide for details.
Creates an event dispatcher that can be used to dispatch component events. Event dispatchers create CustomEvent instances that do not bubble.

Signature

return
EventDispatcher<EventMap>
An event dispatcher function

EventDispatcher

type
string
required
The event name
detail
any
The event detail data. Type depends on the event type in the EventMap
options
DispatchOptions
Event options
options.cancelable
boolean
default:"false"
Whether the event can be cancelled with event.preventDefault()
return
boolean
Returns false if the event was cancelled with preventDefault(), otherwise true

Usage

Basic Event Dispatch

Create and dispatch simple events:

Typed Event Dispatcher

Type your events for better IDE support and type safety:

Cancelable Events

Create events that can be cancelled:
Parent component:

Event Forwarding

Forward events from child components:
Or use the shorthand syntax:

Event Characteristics

No Bubbling

Unlike DOM events, component events created with createEventDispatcher do not bubble:

CustomEvent Details

Dispatched events are CustomEvent instances:

Migration to Svelte 5

In Svelte 5, component events are replaced with callback props:
Parent usage:

Notes

  • Must be called during component initialization
  • Events do not bubble - they only go one level up
  • Events are CustomEvent instances
  • The detail property contains your event data
  • Return value indicates whether the event was cancelled
  • Unlike DOM events, component events are synchronous
  • In Svelte 5, prefer callback props over createEventDispatcher

See Also