Challenge - 5 Problems
Svelte Use Directive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Svelte component?
Consider this Svelte code using the
use: directive. What will be the text content of the <div> after the component mounts?Svelte
<script> function highlight(node) { node.style.backgroundColor = 'yellow'; } </script> <div use:highlight>Hello</div>
Attempts:
2 left
💡 Hint
The
use: directive runs the function with the element as argument after mount.✗ Incorrect
The
use:highlight directive calls the highlight function with the div node, which sets its background color to yellow. The text remains 'Hello'.📝 Syntax
intermediate2:00remaining
Which option correctly uses the
use: directive to add a tooltip?You want to add a tooltip to a button using a
use: directive. Which code snippet is correct?Attempts:
2 left
💡 Hint
The correct syntax is
use:action without parentheses.✗ Incorrect
The
use: directive requires the function name without parentheses or braces. Option C uses use:tooltip correctly.🔧 Debug
advanced2:00remaining
Why does this
use: directive not update when the parameter changes?Look at this Svelte code. The
use:color directive sets the background color. Why does the background not update when color changes?Svelte
<script> export let color = 'red'; function colorAction(node) { node.style.backgroundColor = color; } </script> <div use:colorAction>Box</div> <button on:click={() => color = 'blue'}>Change Color</button>
Attempts:
2 left
💡 Hint
Actions run once when the element is created and do not rerun automatically on variable changes.
✗ Incorrect
The
colorAction function runs once when the element mounts. It does not rerun when color changes. To update, the action must handle parameter changes explicitly.🧠 Conceptual
advanced2:00remaining
What is the purpose of the return value in a
use: action function?In Svelte, a
use: action function can return an object with an update and/or destroy method. What is the main purpose of these methods?Attempts:
2 left
💡 Hint
Think about how actions can respond to changes and clean up resources.
✗ Incorrect
The
update method lets the action respond when parameters change. The destroy method cleans up event listeners or other resources when the element is removed.❓ state_output
expert3:00remaining
What is the final background color after clicking the button twice?
Analyze this Svelte component using a
use: directive with parameters. What is the background color of the <div> after clicking the button twice?Svelte
<script> import { writable } from 'svelte/store'; const colors = ['red', 'green', 'blue']; let index = 0; let currentColor = colors[index]; function colorAction(node, color) { node.style.backgroundColor = color; return { update(newColor) { node.style.backgroundColor = newColor; } }; } function changeColor() { index = (index + 1) % colors.length; currentColor = colors[index]; } </script> <div use:colorAction={currentColor}>Color Box</div> <button on:click={changeColor}>Change Color</button>
Attempts:
2 left
💡 Hint
Each click updates
currentColor and triggers the action's update method.✗ Incorrect
Initial color is 'red'. First click changes to 'green', second click changes to 'blue'. After two clicks, the color is 'blue'.