Challenge - 5 Problems
Svelte Action Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output when using an action with parameters in Svelte?
Consider this Svelte action that changes the background color of an element based on a parameter. What will be the background color of the
after mounting?
function colorAction(node, color) {
node.style.backgroundColor = color;
return {
update(newColor) {
node.style.backgroundColor = newColor;
}
};
}
<div use:colorAction={'red'}>Hello</div>Svelte
function colorAction(node, color) { node.style.backgroundColor = color; return { update(newColor) { node.style.backgroundColor = newColor; } }; } <div use:colorAction={'red'}>Hello</div>
Attempts:
2 left
💡 Hint
Remember that the action receives the parameter as the second argument and applies it immediately.
✗ Incorrect
The action receives 'red' as the color parameter and sets the node's background color to red on mount.
❓ state_output
intermediate2:00remaining
How does updating action parameters affect the DOM in Svelte?
Given this Svelte action and component, what will be the background color of the
after the button is clicked?
function colorAction(node, color) {
node.style.backgroundColor = color;
return {
update(newColor) {
node.style.backgroundColor = newColor;
}
};
}
<script>
let bg = 'blue';
</script>
<div use:colorAction={bg}>Color me</div>
<button on:click={() => bg = 'green'}>Change Color</button>Svelte
function colorAction(node, color) { node.style.backgroundColor = color; return { update(newColor) { node.style.backgroundColor = newColor; } }; } <script> let bg = 'blue'; </script> <div use:colorAction={bg}>Color me</div> <button on:click={() => bg = 'green'}>Change Color</button>
Attempts:
2 left
💡 Hint
Check how the action's update method reacts to parameter changes.
✗ Incorrect
When the variable 'bg' changes, Svelte calls the action's update method with the new value, updating the background color.
📝 Syntax
advanced2:00remaining
Which option correctly defines a Svelte action with parameters and cleanup?
You want to create a Svelte action that logs a message when the element is mounted and removes the event listener on destroy. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Remember to keep a reference to the event handler to remove it later.
✗ Incorrect
Option A correctly stores the handler function and removes it in destroy. Others either miss the handler reference or don't remove the listener properly.
🔧 Debug
advanced2:00remaining
Why does this Svelte action not update when parameters change?
Examine this Svelte action usage:
Why does the border not update when clicking the button?
function borderAction(node, width) {
node.style.border = width + 'px solid black';
}
<script>
let borderWidth = 2;
</script>
<div use:borderAction={borderWidth}>Box</div>
<button on:click={() => borderWidth = 5}>Increase Border</button>Why does the border not update when clicking the button?
Svelte
function borderAction(node, width) { node.style.border = width + 'px solid black'; } <script> let borderWidth = 2; </script> <div use:borderAction={borderWidth}>Box</div> <button on:click={() => borderWidth = 5}>Increase Border</button>
Attempts:
2 left
💡 Hint
Check if the action handles changes to its parameters after initial mount.
✗ Incorrect
Without an update method, the action only runs once on mount and ignores parameter changes.
🧠 Conceptual
expert3:00remaining
What happens if an action's update method returns a new destroy function in Svelte?
In Svelte, an action can return an object with update and destroy methods. What is the effect if the update method returns a new destroy function?
Consider:
Consider:
function exampleAction(node, param) {
const cleanup = () => console.log('cleanup old');
node.style.color = param;
return {
update(newParam) {
cleanup();
node.style.color = newParam;
return () => console.log('cleanup new');
},
destroy() {
console.log('destroy called');
}
};
}Svelte
function exampleAction(node, param) { const cleanup = () => console.log('cleanup old'); node.style.color = param; return { update(newParam) { cleanup(); node.style.color = newParam; return () => console.log('cleanup new'); }, destroy() { console.log('destroy called'); } }; }
Attempts:
2 left
💡 Hint
Think about how Svelte manages cleanup between updates and unmount.
✗ Incorrect
When update returns a function, Svelte treats it as a cleanup function replacing the previous one, calling it before the next update or destroy.