javascript / beginner
Snippet
Dispatching Custom Events
Components can emit custom events to communicate with their parents using createEventDispatcher. This allows child components to send data or signals up the component tree.
snippet.js
javascript
1
2
3
4
5
6
7
8
9
import { createEventDispatcher } from 'svelte';const dispatch = createEventDispatcher();function notifyParent() {dispatch('message', {text: 'Hello from child!'});}
svelte
Breakdown
1
const dispatch = createEventDispatcher();
Initializes the event dispatcher for this component.
2
dispatch('message', { ... });
Fires a custom event named 'message' with a data payload.