Event forwarding lets a component pass events it receives up to its parent. This helps keep components simple and reusable.
Event forwarding in Svelte
<ChildComponent on:eventName /> <!-- Inside ChildComponent.svelte --> <button>Click me</button> <!-- Inside WrapperComponent.svelte --> <ChildComponent on:click />
Use on:eventName on the child component tag to forward events.
The child component must emit the event normally for forwarding to work.
click event from the Button component to the parent.<Button on:click />
input event from a custom input component.<CustomInput on:input />
customEvent from the child.<ChildComponent on:customEvent />
The Button component dispatches a click event when clicked. The Wrapper component listens for this event using on:click and shows an alert. This is event forwarding in action.
<!-- Button.svelte --> <script> import { createEventDispatcher } from 'svelte'; const dispatch = createEventDispatcher(); function handleClick() { dispatch('click'); } </script> <button on:click={handleClick} aria-label="Click me button">Click me</button> <!-- Wrapper.svelte --> <script> import Button from './Button.svelte'; function handleClick() { alert('Button clicked! Event forwarded to Wrapper'); } </script> <Button on:click={handleClick} />
Event forwarding only works if the child component dispatches the event using createEventDispatcher or native DOM events.
You can forward multiple events by adding multiple on:event handlers on the child component.
Use aria-label or other accessibility attributes on interactive elements to help screen readers.
Event forwarding passes events from child to parent components easily.
Use on:eventName on the child component tag to listen and forward events.
This keeps components simple and communication clear.
Practice
Solution
Step 1: Understand event forwarding concept
Event forwarding allows a child component to send events directly to its parent without extra code inside the child.Step 2: Identify the main purpose
This simplifies communication by letting the parent listen to child events as if they came from the parent itself.Final Answer:
Passing events from a child component to its parent component easily -> Option AQuick Check:
Event forwarding = passing events up [OK]
- Thinking it styles components
- Confusing with event stopping
- Assuming it creates new events
click event from a child component <Child /> to its parent in Svelte?Solution
Step 1: Recall event forwarding syntax
In Svelte, forwarding an event means listening on the child component tag withon:eventName.Step 2: Match correct syntax
The correct syntax to listen and forward a click event is<Child on:click />. Other options are invalid or unrelated.Final Answer:
<Child on:click /> -> Option CQuick Check:
Use on:eventName to forward events [OK]
- Using bind:click which is for two-way binding
- Using forward:click which is not valid syntax
- Using use:click which is for actions
<!-- Parent.svelte -->
<Child on:customEvent={handleEvent} />
<script>
function handleEvent(e) {
console.log(e.detail);
}
</script>
<!-- Child.svelte -->
<script>
import { createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher();
function send() {
dispatch('customEvent', 'hello');
}
</script>
<button on:click={send}>Send</button>What will be logged when the button is clicked?
Solution
Step 1: Understand event dispatching in Child
The child dispatches a 'customEvent' with detail 'hello' when the button is clicked.Step 2: Check event forwarding and handling in Parent
The parent listens toon:customEventon the child and callshandleEvent, which logse.detail.Final Answer:
"hello" -> Option DQuick Check:
Dispatched event detail logs "hello" [OK]
- Expecting event object instead of detail
- Thinking event is not forwarded automatically
- Assuming no log happens without explicit forwarding
submit event?<!-- Parent.svelte -->
<Child on:submit={handleSubmit} />
<script>
function handleSubmit() {
alert('Submitted');
}
</script>
<!-- Child.svelte -->
<script>
import { createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher();
function submit() {
dispatch('submit');
}
</script>
<button on:click={submit}>Submit</button>Solution
Step 1: Identify event name conflict
The child dispatches an event named 'submit', which is a native DOM event on buttons and forms.Step 2: Understand event forwarding conflict
Because 'submit' is native, the parent'son:submitmay listen to the native event, not the child's custom event, causing confusion.Final Answer:
The child event name 'submit' conflicts with native DOM event and needs renaming -> Option BQuick Check:
Custom event name conflicts cause forwarding issues [OK]
- Assuming dispatch is incorrect
- Thinking parent must bind instead of on
- Ignoring native event name conflicts
Grandparent > Parent > Child. The Child dispatches a save event. How can you forward this event so the Grandparent can listen to it directly without adding handlers in Parent?Solution
Step 1: Understand event forwarding through intermediate components
Events do not automatically bubble through multiple Svelte components; Parent must forward Child's event.Step 2: Forward event in Parent by listening and re-dispatching
Parent listens tosaveon Child and usescreateEventDispatcherto dispatch the same event upward.Final Answer:
In Parent, listen to save and re-dispatch it with createEventDispatcher -> Option AQuick Check:
Intermediate component must re-dispatch events [OK]
- Assuming events bubble automatically through components
- Trying to listen in grandparent without parent forwarding
- Misusing slot props for event forwarding
Ready to prove it?
Pass the test and this pattern moves unknown
