What is the purpose of the update function in a Svelte action?
The update function lets you respond to changes in the action's parameters. It runs whenever the parameters change, allowing you to update the element or behavior accordingly.
Click to reveal answer
beginner
When is the destroy function called in a Svelte action?
The destroy function is called when the element using the action is removed from the DOM. It is used to clean up resources like event listeners or timers to avoid memory leaks.
Click to reveal answer
intermediate
How do you define an action with update and destroy in Svelte?
You return an object from the action function with optional <code>update</code> and <code>destroy</code> methods. For example:<br><pre>function myAction(node, params) {
// setup code
return {
update(newParams) { /* update logic */ },
destroy() { /* cleanup logic */ }
};
}</pre>
Click to reveal answer
beginner
Why is it important to implement destroy in a Svelte action?
Implementing destroy helps prevent memory leaks by removing event listeners, timers, or other side effects when the element is removed from the page.
Click to reveal answer
beginner
Can the update function in a Svelte action be called multiple times?
Yes, the update function can be called multiple times whenever the parameters passed to the action change, allowing the action to react to new values.
Click to reveal answer
What does the destroy method in a Svelte action do?
AInitializes the action when the element is created
BUpdates the element when parameters change
CAdds event listeners to the element
DCleans up resources when the element is removed
✗ Incorrect
The destroy method is called when the element is removed and is used to clean up resources like event listeners.
When is the update function called in a Svelte action?
AEvery time the element is clicked
BWhenever the action's parameters change
COnly once when the action is first applied
DWhen the element is removed from the DOM
✗ Incorrect
The update function runs whenever the parameters passed to the action change.
Which of these is a valid return value from a Svelte action function?
AAn object with <code>update</code> and <code>destroy</code> methods
BA string describing the action
CA boolean indicating success
DNothing (undefined)
✗ Incorrect
A Svelte action returns an object with optional update and destroy methods to manage lifecycle.
What happens if you do not implement destroy in a Svelte action?
AThere may be memory leaks from leftover event listeners
BThe action will not run at all
CThe <code>update</code> function will not be called
DThe element will not render
✗ Incorrect
Without destroy, cleanup does not happen, which can cause memory leaks.
Which lifecycle event does the update method in a Svelte action correspond to?
AElement creation
BWindow resize
CParameter change
DElement removal
✗ Incorrect
The update method runs when the parameters passed to the action change.
Explain how the update and destroy methods work in a Svelte action and why they are useful.
Think about how an action can react to new data and how it should clean up after itself.
You got /4 concepts.
Describe a real-life example where you would use both update and destroy in a Svelte action.
Imagine adding a custom tooltip or animation that changes with parameters.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the destroy function in a Svelte action?
easy
A. To update the element's attributes when parameters change
B. To initialize the action when the element is created
C. To clean up resources when the element is removed from the DOM
D. To render the element on the page
Solution
Step 1: Understand the role of destroy in Svelte actions
The destroy function is called when the element using the action is removed from the DOM to clean up any side effects or event listeners.
Step 2: Differentiate from other lifecycle functions
update handles parameter changes, and initialization happens in the main action function, not in destroy.
Final Answer:
To clean up resources when the element is removed from the DOM -> Option C
Quick Check:
destroy cleans up on removal = A [OK]
Hint: Destroy cleans up when element leaves the page [OK]
Common Mistakes:
Confusing destroy with update
Thinking destroy initializes the action
Assuming destroy runs on parameter change
2. Which of the following is the correct syntax for returning an update and destroy function from a Svelte action?
A Svelte action returns an object with optional update and destroy methods as properties.
Step 2: Match the correct syntax
return { update: (params) => {}, destroy: () => {} }; correctly returns an object with update and destroy as arrow functions. Other options use invalid syntax or wrong property names.
What happens when the action's parameter changes from 'yellow' to 'lightblue'?
medium
A. The background color changes to 'lightblue'
B. The background color resets to empty string
C. Nothing changes because update is not called
D. The action throws an error
Solution
Step 1: Understand the update function role
The update method runs when the action's parameter changes, here it sets the background color to the new value.
Step 2: Trace the parameter change effect
Changing from 'yellow' to 'lightblue' triggers update, which sets node.style.backgroundColor to 'lightblue'.
Final Answer:
The background color changes to 'lightblue' -> Option A
Quick Check:
Parameter change triggers update = D [OK]
Hint: Update runs on parameter change to update styles [OK]
Common Mistakes:
Thinking destroy runs on parameter change
Assuming update does nothing
Confusing initial setup with update
4. Identify the error in this Svelte action code:
function logClicks(node) {
function handleClick() {
console.log('Clicked');
}
node.addEventListener('click', handleClick);
return {
update() {
node.removeEventListener('click', handleClick);
}
};
}
medium
A. The update function incorrectly removes the event listener on every parameter change
B. The handleClick function is not defined properly
C. The event listener is added multiple times causing duplicates
D. The destroy function is missing to remove the event listener on element removal
Solution
Step 1: Analyze the event listener management
The action adds a click listener but does not clean it up when the element is removed.
Step 2: Check lifecycle methods usage
There is no destroy method to remove the listener on element removal, which can cause memory leaks.
Final Answer:
The destroy function is missing to remove the event listener on element removal -> Option D
Quick Check:
Missing destroy cleanup = C [OK]
Hint: Always clean event listeners in destroy [OK]
Common Mistakes:
Removing listeners in update instead of destroy
Not removing listeners at all
Confusing update with destroy lifecycle
5. You want to create a Svelte action that changes an element's text color based on a parameter and logs a message when the element is removed. Which code correctly implements update and destroy?