Consider this Svelte action that returns an object with a destroy method and a update method. What is the main purpose of returning these methods from an action?
function highlight(node, color) { node.style.backgroundColor = color; return { update(newColor) { node.style.backgroundColor = newColor; }, destroy() { node.style.backgroundColor = ''; } }; }
Think about what happens when the action's parameters change or when the element is removed from the page.
Returning update and destroy methods from a Svelte action lets it react to parameter changes and clean up resources when the element is removed.
Given this Svelte action and component usage, what will be the background color of the div after 2 seconds?
function colorChange(node) { node.style.backgroundColor = 'red'; const timeout = setTimeout(() => { node.style.backgroundColor = 'blue'; }, 1000); return { destroy() { clearTimeout(timeout); } }; } <!-- Component --> <div use:colorChange></div>
Look at the setTimeout inside the action and what it does after 1 second.
The action sets the background to red immediately, then changes it to blue after 1 second using setTimeout. After 2 seconds, the color is blue.
Examine this Svelte action and usage. Why does the update method never get called when the parameter changes?
function toggle(node, active) { node.style.opacity = active ? '1' : '0.5'; return { update(newActive) { node.style.opacity = newActive ? '1' : '0.5'; } }; } <!-- Component --> <script> let isActive = true; setTimeout(() => isActive = false, 1000); </script> <div use:toggle={isActive}></div>
Think about how Svelte calls the update method on actions when parameters change.
Svelte calls the update method only if the action is used with a reactive parameter. Here, the parameter is reactive, but the action is not recreated properly because the parameter is primitive and the action is not re-invoked. Actually, this is a trick: Svelte does call update if the parameter changes, so the problem is elsewhere.
In fact, the code is correct and update will be called. The problem is that the action is missing a destroy method, but that does not prevent update from running. The real issue is that the parameter is primitive and Svelte compares by reference, so update is called.
Therefore, the only plausible cause is that the action is used correctly and update is called. So the correct answer is C: the action parameter is reactive and update is called.
Choose the correct Svelte action syntax that returns both update and destroy methods.
Remember the object literal syntax for returning methods in JavaScript.
Option D correctly returns an object with update and destroy methods using concise method syntax. Option D is also valid JavaScript but less modern. Options B and C have syntax errors.
When a Svelte action returns an object with update and destroy methods, how does Svelte use these methods during the component lifecycle?
Think about when parameters change and when elements are removed in Svelte.
Svelte calls the update method on the returned object whenever the action's parameter changes. It calls destroy when the element using the action is removed from the DOM to clean up.