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
✗ Incorrect
createEventDispatcher returns a function that you use to send custom events from a child to a parent component.
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)
✗ Incorrect
The correct syntax is dispatch('save', data) after creating the dispatcher function.
Where do you import createEventDispatcher from?
A'svelte/internal'
B'svelte'
C'svelte/store'
D'svelte/events'
✗ Incorrect
You import createEventDispatcher directly from the main 'svelte' package.
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} />
✗ Incorrect
The parent uses on:update to listen to the child's custom event.
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
✗ Incorrect
If no parent listens, the event is simply ignored without any error.
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.
Practice
(1/5)
1. What is the main purpose of createEventDispatcher in a Svelte component?
easy
A. To manage component state internally
B. To create a new component dynamically
C. To send custom events from a child component to its parent
D. To style components with CSS variables
Solution
Step 1: Understand the role of createEventDispatcher
This function is used to create a dispatcher that sends events from child to parent components.
Step 2: Identify the correct purpose
Among the options, only sending custom events matches the purpose of createEventDispatcher.
Final Answer:
To send custom events from a child component to its parent -> Option C
Quick Check:
createEventDispatcher sends events to parent [OK]
Hint: Remember: dispatch sends events upward to parent [OK]
Common Mistakes:
Confusing event dispatch with state management
Thinking it creates components
Assuming it styles components
2. Which of the following is the correct way to import and use createEventDispatcher in a Svelte component?
easy
A. import { createEventDispatcher } from 'svelte'; const dispatch = createEventDispatcher();
B. import createEventDispatcher from 'svelte'; const dispatch = createEventDispatcher();
C. import { createEventDispatcher } from 'svelte/internal'; const dispatch = createEventDispatcher();
D. import { dispatchEvent } from 'svelte'; const dispatch = dispatchEvent();
Solution
Step 1: Check the import source
The correct import is from 'svelte' using named import syntax.
Step 2: Confirm usage
Calling createEventDispatcher() returns the dispatch function to send events.
Final Answer:
import { createEventDispatcher } from 'svelte'; const dispatch = createEventDispatcher(); -> Option A
Quick Check:
Correct import and call syntax = import { createEventDispatcher } from 'svelte'; const dispatch = createEventDispatcher(); [OK]
Hint: Always import from 'svelte' with curly braces [OK]
Common Mistakes:
Importing from 'svelte/internal' which is private
Using default import instead of named import
Confusing dispatchEvent with createEventDispatcher
3. Given this Svelte child component code:
import { createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher();
function sendData() {
dispatch('update', { value: 42 });
}
What will the parent component receive when listening with on:update?
medium
A. An event with event.detail containing { value: 42 }
B. An event with event.value equal to 42
C. A string 'update' without data
D. No event is sent because dispatch is not called
Solution
Step 1: Understand dispatch call
The dispatch function sends an event named 'update' with data inside an object as the second argument.
Step 2: Recall event data access
The parent receives the event and accesses data via event.detail, which holds the object { value: 42 }.
Final Answer:
An event with event.detail containing { value: 42 } -> Option A
Quick Check:
Data sent in event.detail = An event with event.detail containing { value: 42 } [OK]
Hint: Event data is always inside event.detail [OK]
Common Mistakes:
Expecting event.value instead of event.detail
Thinking dispatch sends only event name string
Assuming dispatch is not called
4. Identify the error in this Svelte child component code:
A. dispatch cannot send events without a second argument
B. dispatch should be imported from 'svelte/internal'
C. dispatch must be called inside a function
D. Missing parentheses when calling createEventDispatcher
Solution
Step 1: Check createEventDispatcher usage
The code assigns dispatch to createEventDispatcher without calling it, missing parentheses.
Step 2: Understand consequences
Without calling createEventDispatcher(), dispatch is not a function, so dispatch('click') causes an error.
Final Answer:
Missing parentheses when calling createEventDispatcher -> Option D
Quick Check:
createEventDispatcher() must be called [OK]
Hint: Always call createEventDispatcher() with () [OK]
Common Mistakes:
Forgetting parentheses after createEventDispatcher
Importing from wrong module
Thinking second argument is mandatory
5. You want a child Svelte component to send two different events, save and cancel, each with different data. Which is the best way to implement this using createEventDispatcher?
hard
A. Create two dispatchers by calling createEventDispatcher() twice, one for each event
B. Create one dispatcher and call dispatch('save', data) or dispatch('cancel', data) as needed
C. Use dispatch('save_cancel', {saveData, cancelData}) to send both events at once
D. Send events by calling dispatchEvent directly on the DOM element
Solution
Step 1: Understand dispatcher usage
One dispatcher handles all custom events; you specify event names when calling dispatch.
Step 2: Evaluate options
Calling dispatch with different event names ('save' or 'cancel') is the correct pattern.
Final Answer:
Create one dispatcher and call dispatch('save', data) or dispatch('cancel', data) as needed -> Option B
Quick Check:
Single dispatcher handles multiple events [OK]
Hint: One dispatcher can send many event types by name [OK]
Common Mistakes:
Creating multiple dispatchers unnecessarily
Sending combined event instead of separate
Using DOM dispatchEvent instead of Svelte dispatch
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.