Complete the code to define a Svelte action that logs when an element is mounted.
function logAction(node) {
console.log('Element mounted');
return {
[1]() {
console.log('Element removed');
}
};
}The destroy method runs when the element is removed from the DOM.
Complete the code to update the action when the parameter changes.
function colorAction(node, color) {
node.style.color = color;
return {
[1](newColor) {
node.style.color = newColor;
}
};
}The update method runs when the parameter changes, allowing the action to update the element.
Fix the error in the action to properly clean up when the element is removed.
function focusAction(node) {
node.focus();
return {
[1]() {
node.blur();
}
};
}The destroy method is the correct place to clean up when the element is removed.
Fill both blanks to create an action that updates color and cleans up style on destroy.
function styleAction(node, color) {
node.style.color = color;
return {
[1](newColor) {
node.style.color = newColor;
},
[2]() {
node.style.color = '';
}
};
}update changes the color when parameters change, and destroy cleans up the style when the element is removed.
Fill all three blanks to create an action that sets font size, updates it, and cleans up on destroy.
function fontSizeAction(node, size) {
node.style.fontSize = [1];
return {
[2](newSize) {
node.style.fontSize = newSize;
},
[3]() {
node.style.fontSize = '';
}
};
}The initial font size is set using the size parameter. The update method changes it when parameters change, and destroy cleans up when the element is removed.