Complete the code to apply an action to an element in Svelte.
<div use:[1]></div>In Svelte, actions are applied to elements using the use: directive followed by the action name.
Complete the action function to receive the element it is applied to.
function myAction([1]) {
// action code here
}The action function receives the DOM element as the first argument, commonly named node.
Fix the error in the action return to properly clean up when the element is removed.
function myAction(node) {
const handle = () => console.log('clicked');
node.addEventListener('click', handle);
return [1];
}The action should return an object with a destroy method to remove event listeners when the element is removed.
Fill both blanks to update the action when parameters change and clean up properly.
function myAction(node, [1]) { function update(params) { console.log(params); } update([2]); return { update, destroy() { // cleanup code } }; }
The second argument is usually named params and the initial parameters are passed to update to initialize the action.
Fill all three blanks to create a reusable action that adds a CSS class and removes it on destroy.
function addClass(node, [1]) { node.classList.add([2]); return { destroy() { node.classList.[3]([2]); } }; }
The action takes a className parameter, adds it to the element, and removes it on destroy using remove.