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
✗ Incorrect
The use: directive applies a custom action function to an element to add behavior.
How do you pass parameters to a use: action?
Ause:action={params}
Buse:action(params)
Cuse:action:param
Duse:action->params
✗ Incorrect
Parameters are passed using curly braces: use: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
✗ Incorrect
Returning an object with a destroy() method lets Svelte clean up the action.
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
✗ Incorrect
The update method runs when the parameters passed to the action change.
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}>
✗ 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.
Practice
(1/5)
1. Which Svelte directive is used to conditionally show content based on a boolean value? {#if}, {#each}, on:, or bind:?
easy
A. bind:
B. {#if}
C. on:
D. {#each}
Solution
Step 1: Understand the purpose of {#if}
The {#if} directive in Svelte shows or hides content based on a condition.
Step 2: Compare with other directives
{#each} loops over lists, on: handles events, and bind: links variables to inputs.
Final Answer:
{#if} -> Option B
Quick Check:
Conditional display = {#if} [OK]
Hint: Use {#if} to show/hide content based on true/false [OK]
Common Mistakes:
Confusing {#if} with {#each} for loops
Using on:click for conditional display
Trying to bind variables for conditionals
2. Which of the following is the correct syntax to listen for a click event on a button in Svelte?
easy
A. <button onClick={handleClick}>Click</button>
B. <button click={handleClick}>Click</button>
C. <button on:click={handleClick}>Click</button>
D. <button bind:click={handleClick}>Click</button>
Solution
Step 1: Recall Svelte event directive syntax
Svelte uses on:eventName={handler} to listen to events like clicks.
Step 2: Check each option
<button on:click={handleClick}>Click</button> uses on:click correctly. Options A, B, and D use incorrect or invalid syntax.
Final Answer:
<button on:click={handleClick}>Click</button> -> Option C
Quick Check:
Event listener syntax = on:event [OK]
Hint: Use on:eventName={handler} to listen for events [OK]
Common Mistakes:
Using camelCase like onClick instead of on:click
Trying to bind events with bind:
Omitting the on: prefix
3. What will be the rendered output of this Svelte code?
<script>
let items = ['apple', 'banana'];
</script>
<ul>
{#each items as item}
<li>{item}</li>
{/each}
</ul>
medium
A.
apple
banana
B.
items
C.
{item}
{item}
D. Syntax error
Solution
Step 1: Understand the {#each} directive
The {#each} directive loops over the array items and assigns each element to item.
Step 2: Render list items
For each item, it creates a <li> element with the item text inside.
Final Answer:
<ul><li>apple</li><li>banana</li></ul> -> Option A
Quick Check:
Loop items render as list =
apple
banana
[OK]
Hint: Each loops output list items for each array element [OK]
Common Mistakes:
Expecting the literal string {item} to show
Confusing the array name with item variable
Thinking it causes a syntax error
4. Identify the error in this Svelte snippet:
<script>
let count = 0;
</script>
<button on:click="count++">Increment</button>
<p>Count: {count}</p>
medium
A. Using double quotes around the event handler expression
B. Missing bind: directive on count variable
C. Incorrect variable name count
D. No error, code works fine
Solution
Step 1: Check event handler syntax
Svelte requires event handlers to use curly braces without quotes: on:click={count++}.
Step 2: Identify the error
The code uses quotes around the expression, which causes a syntax error.
Final Answer:
Using double quotes around the event handler expression -> Option A
Quick Check:
Event handler must be unquoted expression [OK]
Hint: Use curly braces, not quotes, for event handlers [OK]
Common Mistakes:
Putting event handler code inside quotes
Trying to bind variables unnecessarily
Assuming variable name is wrong
5. You want to create a Svelte component that shows a list of tasks and lets the user toggle each task's completion status by clicking a checkbox. Which directive syntax combination correctly binds the checkbox state and updates the task's done property?