What is a default action in Svelte event handling?
A default action is the browser's built-in behavior that happens when an event occurs, like submitting a form or following a link when clicked.
Click to reveal answer
beginner
How do you prevent a default action in Svelte?
You can prevent a default action by adding |preventDefault modifier to the event handler, like on:submit|preventDefault.
Click to reveal answer
beginner
What happens if you don't prevent the default action on a form submit in Svelte?
The browser will reload the page or navigate away, which usually interrupts your app's behavior.
Click to reveal answer
intermediate
Can you combine multiple event modifiers in Svelte? Give an example with default action prevention.
Yes, you can combine modifiers like on:click|preventDefault|stopPropagation to prevent default action and stop event bubbling.
Click to reveal answer
beginner
Why is it important to handle default actions properly in Svelte apps?
Handling default actions properly keeps your app in control of user interactions, avoids unwanted page reloads, and improves user experience.
Click to reveal answer
Which Svelte event modifier prevents the browser's default action?
A|preventDefault
B|stopPropagation
C|capture
D|once
✗ Incorrect
The |preventDefault modifier stops the browser's default behavior for the event.
What is the default action when a form is submitted without preventing it?
AThe form data is logged to console
BNothing happens
CThe page reloads or navigates away
DThe form resets automatically
✗ Incorrect
Submitting a form normally causes the browser to reload or navigate, which can interrupt your app.
How do you write a click event handler in Svelte that prevents default and stops propagation?
Aon:click.preventDefault.stop
Bon:click|preventDefault|stopPropagation
Con:click.preventDefault.stopPropagation
Don:click.prevent|stop
✗ Incorrect
Svelte uses pipe-separated modifiers like |preventDefault and |stopPropagation together.
If you want a link not to navigate when clicked, what should you do in Svelte?
AAdd on:click|preventDefault to the link
BRemove the href attribute
CUse on:click without modifiers
DUse on:click|stopPropagation only
✗ Incorrect
Using |preventDefault on the click event stops the browser from following the link.
What does the |stopPropagation modifier do in Svelte event handling?
ACaptures the event before it reaches the target
BPrevents the default browser action
CRuns the event handler only once
DStops the event from bubbling up to parent elements
✗ Incorrect
|stopPropagation stops the event from moving up the DOM tree to parent elements.
Explain what default actions are in Svelte and how you can control them.
Think about what happens when you submit a form or click a link in the browser.
You got /3 concepts.
Describe how to combine event modifiers in Svelte to prevent default actions and stop event propagation.
Modifiers are added with pipes | after the event name.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of a default action in Svelte?
easy
A. To add behavior to an element when it is created and clean up when removed
B. To style an element with CSS automatically
C. To create a new component dynamically
D. To fetch data from an API when the page loads
Solution
Step 1: Understand what default actions do
Default actions in Svelte run code when an element is created and can clean up when it is removed.
Step 2: Match purpose with options
Only To add behavior to an element when it is created and clean up when removed describes adding behavior on creation and cleanup on removal, which matches default actions.
Final Answer:
To add behavior to an element when it is created and clean up when removed -> Option A
Quick Check:
Default actions add and clean behavior = B [OK]
Hint: Default actions run on element mount and cleanup on unmount [OK]
Common Mistakes:
Confusing default actions with styling or data fetching
Thinking default actions create components
Assuming default actions only run once globally
2. Which of the following is the correct syntax to apply a default action named focus to an input element in Svelte?
easy
A. <input on:focus />
B. <input action:focus />
C. <input bind:focus />
D. <input use:focus />
Solution
Step 1: Recall Svelte default action syntax
Default actions are applied using use:actionName on elements.
Step 2: Compare options with correct syntax
<input use:focus /> uses use:focus, which is the correct way to apply the action.
Final Answer:
<input use:focus /> -> Option D
Quick Check:
Default action syntax = use:actionName [OK]
Hint: Use use: prefix to apply default actions [OK]
What will be the background color of the <div> when it is rendered?
medium
A. Yellow
B. No background color (default)
C. Red
D. Transparent
Solution
Step 1: Analyze the highlight action code
The action sets backgroundColor to 'yellow' on the node when applied.
Step 2: Understand the effect on the <div>
The <div> uses use:highlight, so its background color becomes yellow on render.
Final Answer:
Yellow -> Option A
Quick Check:
Action sets background to yellow = A [OK]
Hint: Default action runs on mount, sets style immediately [OK]
Common Mistakes:
Thinking destroy runs immediately and clears color
Confusing background color with text color
Assuming no style changes without CSS
4. Given this default action code:
function logClick(node) {
node.addEventListener('click', () => console.log('Clicked!'));
}
What is the main problem with this action?
medium
A. It should return a promise to work correctly
B. It uses an arrow function which is not allowed in actions
C. It does not remove the event listener when the element is removed
D. It must use use:logClick() with parentheses
Solution
Step 1: Check event listener management
The action adds a click listener but does not remove it on cleanup.
Step 2: Understand Svelte action cleanup
Actions should return an object with a destroy method to remove listeners and avoid memory leaks.
Final Answer:
It does not remove the event listener when the element is removed -> Option C
Quick Check:
Missing cleanup causes leaks = A [OK]
Hint: Always return destroy() to clean event listeners [OK]
Common Mistakes:
Ignoring cleanup leads to memory leaks
Thinking arrow functions are disallowed
Assuming parentheses are required on use
5. You want to create a default action autoFocus that focuses an input element when it appears and logs "Focused" once. Which code correctly implements this behavior?