0
0
Svelteframework~20 mins

Event forwarding in Svelte - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Event Forwarding Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a forwarded event is triggered?

Consider a Svelte component Child.svelte that forwards a click event to its parent. What will the parent component receive when the child is clicked?

Svelte
<!-- Child.svelte -->
<script>
  import { createEventDispatcher } from 'svelte';
  const dispatch = createEventDispatcher();
  function handleClick() {
    dispatch('click');
  }
</script>
<button on:click={handleClick}>Click me</button>

<!-- Parent.svelte -->
<script>
  import Child from './Child.svelte';
  function onChildClick() {
    alert('Child clicked!');
  }
</script>
<Child on:click={onChildClick} />
AThe parent receives the click event and runs onChildClick, showing an alert.
BThe parent does not receive any event because events do not bubble in Svelte.
CThe child component's click event is blocked and does not trigger any handler.
DThe parent receives a custom event named 'handleClick' instead of 'click'.
Attempts:
2 left
💡 Hint

Think about how createEventDispatcher works and how events are forwarded in Svelte.

📝 Syntax
intermediate
2:00remaining
Which syntax correctly forwards all events from a child component?

You want a Svelte child component to forward all events it receives to its parent. Which syntax achieves this?

Svelte
<!-- Child.svelte -->
<script>
  // Forward all events
</script>
<div>Content</div>
A<svelte:options forwardEvents />
B<svelte:options accessors={true} />
C<svelte:options tag="forward" />
D<svelte:options accessors={false} />
Attempts:
2 left
💡 Hint

Look for the special Svelte tag that controls event forwarding.

🔧 Debug
advanced
2:00remaining
Why does the parent not receive the forwarded event?

Given this child component code, the parent does not receive the custom event. What is the cause?

Svelte
<!-- Child.svelte -->
<script>
  import { createEventDispatcher } from 'svelte';
  const dispatch = createEventDispatcher();
  function sendEvent() {
    dispatch('custom');
  }
</script>
<button on:click={sendEvent}>Send</button>

<!-- Parent.svelte -->
<script>
  import Child from './Child.svelte';
</script>
<Child on:custom={() => alert('Received')} />
AThe parent must listen to 'click' instead of 'custom' to receive the event.
BThe event name 'custom' is reserved and cannot be forwarded.
CThe child component does not forward the event because <code>&lt;svelte:options forwardEvents /&gt;</code> is missing.
DThe child component's dispatch function is not imported correctly.
Attempts:
2 left
💡 Hint

Check if the child component is set to forward events automatically.

state_output
advanced
2:00remaining
What is the output when forwarding a custom event with detail?

In Svelte, a child component dispatches a custom event with detail data. What will the parent receive?

Svelte
<!-- Child.svelte -->
<script>
  import { createEventDispatcher } from 'svelte';
  const dispatch = createEventDispatcher();
  function sendData() {
    dispatch('update', { detail: { count: 5 } });
  }
</script>
<button on:click={sendData}>Send</button>

<!-- Parent.svelte -->
<script>
  import Child from './Child.svelte';
  let receivedCount = 0;
  function handleUpdate(event) {
    receivedCount = event.detail.count;
  }
</script>
<Child on:update={handleUpdate} />
<p>{receivedCount}</p>
AThe code throws a runtime error because 'detail' is undefined.
BThe paragraph remains '0' because detail is not forwarded.
CThe paragraph shows '[object Object]' instead of a number.
DThe paragraph shows '5' after clicking the button.
Attempts:
2 left
💡 Hint

Remember how event details are accessed in Svelte event handlers.

🧠 Conceptual
expert
2:00remaining
Why use event forwarding instead of direct prop callbacks in Svelte?

Which reason best explains why event forwarding is preferred over passing callback functions as props for child-to-parent communication in Svelte?

APassing callbacks as props is not supported in Svelte and causes syntax errors.
BEvent forwarding allows decoupling components and supports native event handling patterns, improving reusability and accessibility.
CEvent forwarding automatically serializes data, while callbacks do not work with complex objects.
DUsing callbacks requires extra libraries, but event forwarding is built-in.
Attempts:
2 left
💡 Hint

Think about component design and how events relate to accessibility and reusability.