📖 Scenario: You are building a simple user interface with two components: a Button component and a App component. The Button component will emit a click event, and the App component will listen to that event.To keep your code clean, you want the Button component to forward the click event it receives to its parent App component.
🎯 Goal: Create a Button.svelte component that forwards the click event to its parent. Then use this component inside App.svelte and handle the forwarded event to update a counter.
📋 What You'll Learn
Create a Button.svelte component with a button element
Forward the click event from Button.svelte to its parent
Create an App.svelte component that uses Button
Handle the forwarded click event in App.svelte to increment a counter
Display the current count in App.svelte
💡 Why This Matters
🌍 Real World
Event forwarding is useful when building reusable UI components that need to communicate user actions to parent components without exposing internal details.
💼 Career
Understanding event forwarding is important for frontend developers working with component-based frameworks like Svelte, React, or Vue to build clean and maintainable code.
Progress0 / 4 steps
1
Create the Button component with a button element
Create a Svelte component file named Button.svelte. Inside it, write a <button> element with the text Click me.
Svelte
Hint
Start by writing a simple button element with the text 'Click me' inside your Button.svelte file.
2
Forward the click event from Button component
In Button.svelte, import createEventDispatcher from 'svelte'. Create a dispatcher with const dispatch = createEventDispatcher(). Add an event handler to the <button> that calls dispatch('click') when the button is clicked.
Svelte
Hint
Use createEventDispatcher to forward the click event from the button to the parent component.
3
Use Button component in App and handle forwarded click
Create App.svelte. Import Button from './Button.svelte'. Create a variable count initialized to 0. Add the <Button /> component with an event listener on:click that increments count by 1.
Svelte
Hint
Import the Button component and use it. Listen for the click event to update the count variable.
4
Display the click count in App component
In App.svelte, below the <Button />, add a paragraph element <p> that shows the text Clicked {count} times to display the current count.
Svelte
Hint
Use a paragraph element to show how many times the button was clicked by displaying the count variable.
Practice
(1/5)
1. What does event forwarding in Svelte primarily help with?
easy
A. Passing events from a child component to its parent component easily
B. Automatically styling components based on events
C. Preventing events from bubbling up the DOM tree
D. Creating new custom events inside a component
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 A
Quick Check:
Event forwarding = passing events up [OK]
Hint: Event forwarding means child events reach parent directly [OK]
Common Mistakes:
Thinking it styles components
Confusing with event stopping
Assuming it creates new events
2. Which syntax correctly forwards a click event from a child component <Child /> to its parent in Svelte?
easy
A. <Child forward:click />
B. <Child bind:click />
C. <Child on:click />
D. <Child use:click />
Solution
Step 1: Recall event forwarding syntax
In Svelte, forwarding an event means listening on the child component tag with on: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 C
Quick Check:
Use on:eventName to forward events [OK]
Hint: Use on:eventName on child tag to forward events [OK]
A. The parent must use bind:submit instead of on:submit
B. The child event name 'submit' conflicts with native DOM event and needs renaming
C. The dispatch function is not called correctly
D. The parent forgot to import Child component
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's on:submit may 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 B
Quick Check:
Custom event name conflicts cause forwarding issues [OK]
Hint: Avoid native event names for custom events [OK]
Common Mistakes:
Assuming dispatch is incorrect
Thinking parent must bind instead of on
Ignoring native event name conflicts
5. You have a nested component structure: 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?
hard
A. In Parent, listen to save and re-dispatch it with createEventDispatcher
B. In Parent, forward the save event by adding <Child on:save /> and export let $$restProps with <slot {...$$restProps} />
C. In Grandparent, import Child directly and listen to save
D. In Child, dispatch save with option
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 to save on Child and uses createEventDispatcher to dispatch the same event upward.
Final Answer:
In Parent, listen to save and re-dispatch it with createEventDispatcher -> Option A
Quick Check:
Intermediate component must re-dispatch events [OK]
Hint: Intermediate must re-dispatch events to forward [OK]
Common Mistakes:
Assuming events bubble automatically through components
Trying to listen in grandparent without parent forwarding
Misusing slot props for event forwarding
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.