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?✗ Incorrect
The
use: directive applies a custom action function to an element to add behavior.How do you pass parameters to a
use: action?✗ Incorrect
Parameters are passed using curly braces:
use:action={params}.What should a
use: action return to clean up when the element is removed?✗ Incorrect
Returning an object with a
destroy() method lets Svelte clean up the action.When is the
update method of a use: action called?✗ Incorrect
The
update method runs when the parameters passed to the action change.Which of these is a valid use of the
use: directive?✗ Incorrect
The correct syntax to pass parameters is
use:action={params}.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.