0
0
Svelteframework~20 mins

Component events (createEventDispatcher) in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Svelte Event Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when the child component dispatches an event?

Consider a Svelte child component that uses createEventDispatcher to send a 'notify' event with a message. The parent listens to this event. What will the parent display after the child dispatches?

Svelte
/* Child.svelte */
<script>
  import { createEventDispatcher } from 'svelte';
  const dispatch = createEventDispatcher();
  function send() {
    dispatch('notify', { message: 'Hello from child' });
  }
</script>
<button on:click={send}>Send</button>

<!-- Parent.svelte -->
<script>
  import Child from './Child.svelte';
  let msg = '';
  function handleNotify(event) {
    msg = event.detail.message;
  }
</script>
<Child on:notify={handleNotify} />
<p>{msg}</p>
A<p>Hello from child</p>
B<p>undefined</p>
C<p></p>
DRuntime error: event.detail is undefined
Attempts:
2 left
💡 Hint

Remember that dispatch sends an event with a detail property containing the data.

📝 Syntax
intermediate
1:30remaining
Which option correctly dispatches a custom event with data in Svelte?

Choose the correct syntax to dispatch a 'change' event with a payload { value: 42 } using createEventDispatcher.

Svelte
import { createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher();

// Which line correctly dispatches the event?
Adispatch('change', [value: 42]);
Bdispatch('change', value: 42);
Cdispatch('change', value = 42);
Ddispatch('change', { value: 42 });
Attempts:
2 left
💡 Hint

The second argument to dispatch must be an object with the data.

🔧 Debug
advanced
2:30remaining
Why does the parent not receive the dispatched event?

A child component dispatches a 'submit' event, but the parent never reacts. What is the likely cause?

Svelte
<!-- Child.svelte -->
<script>
  import { createEventDispatcher } from 'svelte';
  const dispatch = createEventDispatcher();
  function submit() {
    dispatch('submit', { data: 123 });
  }
</script>
<button on:click={submit}>Submit</button>

<!-- Parent.svelte -->
<script>
  import Child from './Child.svelte';
  let received = false;
  function onSubmit() {
    received = true;
  }
</script>
<Child on:submit={onSubmit} />
<p>{received ? 'Event received' : 'Waiting...'}</p>
AThe child dispatches 'submit' but the parent component is missing the import of Child.
BThe parent uses 'on:submit' but the child dispatches 'submit' event correctly; issue is elsewhere.
CThe child dispatches 'submit' but the parent listens to 'onsubmit' (wrong event name).
DThe parent event handler 'onSubmit' does not accept the event parameter, causing failure.
Attempts:
2 left
💡 Hint

Check if the parent component properly imports and uses the child component.

state_output
advanced
2:00remaining
What is the value of count after dispatching an event?

In this Svelte parent component, the child dispatches an 'increment' event. The parent updates count accordingly. What is the final value of count after clicking the button once?

Svelte
<!-- Child.svelte -->
<script>
  import { createEventDispatcher } from 'svelte';
  const dispatch = createEventDispatcher();
  function inc() {
    dispatch('increment', { amount: 5 });
  }
</script>
<button on:click={inc}>Add 5</button>

<!-- Parent.svelte -->
<script>
  import Child from './Child.svelte';
  let count = 0;
  function handleIncrement(event) {
    count += event.detail.amount;
  }
</script>
<Child on:increment={handleIncrement} />
<p>{count}</p>
ANaN
B0
C5
Dundefined
Attempts:
2 left
💡 Hint

The event detail contains the amount to add to count.

🧠 Conceptual
expert
3:00remaining
Why use createEventDispatcher instead of props for child-to-parent communication?

In Svelte, what is the main advantage of using createEventDispatcher for child-to-parent communication compared to passing callback functions as props?

AProps cannot pass functions in Svelte, so events are the only way.
BEvents allow decoupling so the child does not need to know about the parent's implementation details.
CUsing events is faster because props cause unnecessary re-renders.
DEvents automatically update the parent's state without handlers.
Attempts:
2 left
💡 Hint

Think about how events help keep components independent.