javascript / beginner
Snippet
Dispatching Custom Events
Components can communicate back to their parents by dispatching custom events. The createEventDispatcher function creates a helper to send data upwards.
snippet.js
1
2
3
4
5
6
7
import { createEventDispatcher } from 'svelte';const dispatch = createEventDispatcher();function notify() {dispatch('message', { text: 'Hello!' });}
svelte
Breakdown
1
const dispatch = createEventDispatcher();
Initializes the event dispatcher for the current component instance.
2
dispatch('message', { ... });
Sends an event named 'message' with an optional detail object containing data.