A named action in Svelte is a reusable function that you can apply to HTML elements to add behavior, like event handling or animations, by using the use: directive with a specific name.
Click to reveal answer
beginner
How do you apply a named action to an element in Svelte?
You apply a named action by adding use:actionName to the HTML element, where actionName is the function you want to run on that element.
Click to reveal answer
intermediate
What arguments does a Svelte action function receive?
A Svelte action function receives two arguments: the HTML element it is applied to, and an optional parameter object for configuration.
Click to reveal answer
intermediate
What should a Svelte action return if it needs to clean up?
If cleanup is needed, the action should return an object with a destroy method that runs when the element is removed or the action is changed.
Click to reveal answer
intermediate
Can named actions in Svelte accept parameters? How?
Yes, named actions can accept parameters by passing them like use:actionName={params}. The action function receives these parameters as the second argument.
Click to reveal answer
How do you attach a named action called highlight to a <div> in Svelte?
A<div use:highlight></div>
B<div action:highlight></div>
C<div on:highlight></div>
D<div highlight></div>
✗ Incorrect
In Svelte, named actions are attached using the use: directive followed by the action name.
What does a Svelte action function receive as its first argument?
AAn event object
BThe HTML element it is applied to
CThe component instance
DThe action's name
✗ Incorrect
The first argument to a Svelte action function is always the HTML element the action is applied to.
What should a Svelte action return if it needs to clean up when the element is removed?
AA promise
BA boolean true
CNothing, cleanup is automatic
DAn object with a destroy() method
✗ Incorrect
Returning an object with a destroy method lets Svelte know how to clean up when the element is removed.
How do you pass parameters to a named action in Svelte?
Ause:actionName={params}
Buse:actionName(params)
CactionName={params}
Don:actionName={params}
✗ Incorrect
Parameters are passed to named actions using curly braces after the action name, like use:actionName={params}.
Which of these is NOT a valid use of a named action in Svelte?
Ause:fade
Buse:tooltip={text}
Cuse:onClick
Duse:draggable
✗ Incorrect
use:onClick is invalid because event handlers use on:, not use:.
Explain how named actions work in Svelte and how you would create and use one.
Think about how you add behavior to elements with functions.
You got /4 concepts.
Describe how to pass parameters to a named action and how the action function handles them.
Consider how you customize the action's effect on the element.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of named actions in Svelte?
easy
A. To style elements with CSS
B. To define new components
C. To add reusable behavior to HTML elements
D. To manage application state
Solution
Step 1: Understand what named actions do
Named actions in Svelte are used to add behavior to HTML elements that can be reused across components.
Step 2: Compare with other options
Defining components, styling, and state management are handled differently in Svelte, not by named actions.
Final Answer:
To add reusable behavior to HTML elements -> Option C
Quick Check:
Named actions = reusable element behavior [OK]
Hint: Named actions add behavior to elements, not styles or state [OK]
Common Mistakes:
Confusing actions with components
Thinking actions handle styling
Assuming actions manage state
2. Which of the following is the correct syntax to define a named action in Svelte?
easy
A. function myAction(node) { /* code */ }
B. const myAction = () => { /* code */ }
C. let myAction = node => { /* code */ }
D. action myAction(node) { /* code */ }
Solution
Step 1: Recall named action syntax
Named actions are functions that receive the element node as the first argument, typically declared as function myAction(node) {}.
Step 2: Check other options
Arrow functions can work but must return an object for cleanup; action myAction(node) { /* code */ } uses invalid syntax.
Final Answer:
function myAction(node) { /* code */ } -> Option A
Quick Check:
Named action = function with node parameter [OK]
Hint: Named actions are functions with node parameter, not keywords [OK]
A. Calling node.blur() in destroy may cause errors if node is removed
B. The action should not call node.focus() immediately
C. The action does not return an object
D. The action must be async to work properly
Solution
Step 1: Analyze the focus call timing
Calling node.focus() immediately may not work reliably because the element might not be fully rendered, visible, or ready (e.g., during CSS transitions or animations).
Step 2: Best practice for focus actions
In Svelte, it's often necessary to delay the focus call using tick(), requestAnimationFrame, or setTimeout(..., 0) to ensure DOM readiness.
Final Answer:
The action should not call node.focus() immediately -> Option B
Quick Check:
Delay focus with tick() or RAF [OK]
Hint: Delay node.focus() with tick() for reliability [OK]
Common Mistakes:
Calling focus immediately without delay
Ignoring DOM/transition timing issues
Assuming focus always succeeds synchronously
5. You want to create a named action that changes an element's text color based on a parameter and updates it when the parameter changes. Which approach is correct?
hard
A. Use a reactive statement inside the action function to watch parameters
B. Set the color once in the action and ignore parameter changes
C. Recreate the action every time parameters change instead of updating
D. Return an update function from the action that sets the color when parameters change
Solution
Step 1: Understand parameter updates in named actions
Named actions can return an update function to handle parameter changes dynamically.
Step 2: Evaluate options for handling parameter changes
Setting color once ignores updates; reactive statements inside action function don't track parameters; recreating action is inefficient.
Final Answer:
Return an update function from the action that sets the color when parameters change -> Option D
Quick Check:
Use update() to handle parameter changes [OK]
Hint: Use update() function to react to parameter changes [OK]
Common Mistakes:
Ignoring parameter updates
Misusing reactive statements inside actions
Recreating actions unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.