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?
<!-- 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} />
Think about how createEventDispatcher works and how events are forwarded in Svelte.
Using createEventDispatcher in the child component allows it to send a custom event named 'click' that the parent can listen to. When the child button is clicked, the parent receives the event and runs the handler.
You want a Svelte child component to forward all events it receives to its parent. Which syntax achieves this?
<!-- Child.svelte -->
<script>
// Forward all events
</script>
<div>Content</div>Look for the special Svelte tag that controls event forwarding.
The <svelte:options forwardEvents /> directive tells Svelte to forward all events from the child component to its parent automatically.
Given this child component code, the parent does not receive the custom event. What is the cause?
<!-- 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')} />
Check if the child component is set to forward events automatically.
Without <svelte:options forwardEvents />, the child component's events are not forwarded automatically. The parent won't receive the 'custom' event unless it is forwarded explicitly.
In Svelte, a child component dispatches a custom event with detail data. What will the parent receive?
<!-- 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>
Remember how event details are accessed in Svelte event handlers.
The dispatched event includes a detail object with count: 5. The parent handler reads event.detail.count and updates the variable, which updates the paragraph.
Which reason best explains why event forwarding is preferred over passing callback functions as props for child-to-parent communication in Svelte?
Think about component design and how events relate to accessibility and reusability.
Event forwarding follows native event patterns, making components more modular and easier to maintain. It also supports accessibility features like keyboard navigation and ARIA roles better than passing callbacks as props.