0
0
Svelteframework~5 mins

Component events (createEventDispatcher) in Svelte - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is createEventDispatcher used for in Svelte?
It is used to create a function that lets a child component send custom events to its parent component.
Click to reveal answer
beginner
How do you import <code>createEventDispatcher</code> in a Svelte component?
You import it from 'svelte' like this: <br><code>import { createEventDispatcher } from 'svelte';</code>
Click to reveal answer
intermediate
Explain the steps to dispatch a custom event from a child component.
1. Import <code>createEventDispatcher</code>.<br>2. Call it to get a <code>dispatch</code> function.<br>3. Use <code>dispatch('eventName', detail)</code> to send the event.<br>4. The parent listens for <code>on:eventName</code>.
Click to reveal answer
beginner
What does the detail argument in dispatch('eventName', detail) represent?
It is an optional object or value that carries extra information about the event to the parent component.
Click to reveal answer
beginner
How does a parent component listen to a custom event dispatched by a child?
The parent adds an event listener using on:eventName on the child component tag, like <Child on:eventName={handler} />.
Click to reveal answer
What does createEventDispatcher return?
AAn event listener
BA component instance
CA function to send custom events
DA DOM element
How do you send a custom event named 'save' with data from a child component?
AsendEvent('save', data)
BdispatchEvent('save', data)
Cemit('save', data)
Ddispatch('save', data)
Where do you import createEventDispatcher from?
A'svelte/internal'
B'svelte'
C'svelte/store'
D'svelte/events'
How does a parent component listen to a custom event 'update' from a child?
A<Child on:update={handler} />
B<Child listen:update={handler} />
C<Child event:update={handler} />
D<Child catch:update={handler} />
What happens if you dispatch an event without a listener in the parent?
ANothing, the event is ignored
BAn error is thrown
CThe app crashes
DThe event bubbles to the DOM
Describe how to create and use a custom event in Svelte using createEventDispatcher.
Think about how child and parent communicate with events.
You got /4 concepts.
    Explain the role of the detail parameter when dispatching events in Svelte.
    What extra information might the parent need?
    You got /3 concepts.