javascript / intermediate
Snippet
Communication via Component Event Dispatchers
Svelte's event dispatcher allows components to emit custom events to their parents. Unlike standard DOM events, these do not bubble automatically unless explicitly forwarded.
snippet.js
1
2
3
4
5
6
7
8
9
10
import { createEventDispatcher } from 'svelte';const dispatch = createEventDispatcher();function completeTask(id) {dispatch('complete', {taskId: id,timestamp: Date.now()});}
svelte
Breakdown
1
const dispatch = createEventDispatcher();
Initialize the dispatcher; it must be called during component initialization.
2
dispatch('complete', { ... });
Emit a custom event named 'complete' with a detail object containing task data.