0
0
Svelteframework~5 mins

Use directive syntax in Svelte - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the use: directive in Svelte?
The use: directive lets you apply a custom action to an HTML element. Actions are functions that run when the element is created and can add behavior like event listeners or animations.
Click to reveal answer
beginner
How do you define a custom action to use with use: in Svelte?
A custom action is a function that receives the element as its first argument and optionally returns an object with a destroy method to clean up when the element is removed.
Click to reveal answer
beginner
Example: What does <div use:highlight> do if highlight is a custom action?
It applies the highlight action to the div element, which might add styles or behavior like changing background color when the element appears.
Click to reveal answer
intermediate
Can use: directives accept parameters? How?
Yes. You can pass parameters by writing use:action={params}. The action function receives the element and the parameters, allowing dynamic behavior.
Click to reveal answer
intermediate
What happens if a use: action returns an object with an update method?
The update method is called whenever the parameters change, letting the action respond to new values without recreating the element.
Click to reveal answer
What does the use: directive do in Svelte?
ACreates a reactive statement
BBinds a variable to an input
CApplies a custom action to an element
DImports a component
How do you pass parameters to a use: action?
Ause:action={params}
Buse:action(params)
Cuse:action:param
Duse:action->params
What should a use: action return to clean up when the element is removed?
AA function named cleanup
BA boolean true
CNothing
DAn object with a destroy() method
When is the update method of a use: action called?
AWhen the parameters change
BWhen the element is clicked
CWhen the component mounts
DWhen the page reloads
Which of these is a valid use of the use: directive?
A<div use:tooltip-text>
B<div use:tooltip={text}>
C<div use:tooltip(text)>
D<div use-tooltip={text}>
Explain how to create and use a custom action with the use: directive in Svelte.
Think about how you add behavior to an element when it appears.
You got /4 concepts.
    Describe what happens when a use: action returns an object with destroy and update methods.
    Consider lifecycle of the element and parameter changes.
    You got /4 concepts.