Actions let you add extra behavior to HTML elements that you can use again and again. They keep your code clean and easy to manage.
Why actions add reusable element behavior in Svelte
function myAction(node, parameters) { // setup code here return { update(newParameters) { // handle parameter changes }, destroy() { // cleanup code here } }; }
The node is the HTML element the action is applied to.
The function can return an object with update and destroy to handle changes and cleanup.
function focus(node) {
node.focus();
}function highlight(node, color) { node.style.backgroundColor = color; return { update(newColor) { node.style.backgroundColor = newColor; }, destroy() { node.style.backgroundColor = ''; } }; }
This example shows a tooltip action that adds a small popup text when you hover over the button. The action handles showing and hiding the tooltip and cleans up event listeners when the button is removed.
<script> function tooltip(node, text) { let tooltipDiv; function mouseOver() { tooltipDiv = document.createElement('div'); tooltipDiv.textContent = text; tooltipDiv.style.position = 'absolute'; tooltipDiv.style.background = 'black'; tooltipDiv.style.color = 'white'; tooltipDiv.style.padding = '0.25rem 0.5rem'; tooltipDiv.style.borderRadius = '0.25rem'; tooltipDiv.style.top = `${node.getBoundingClientRect().bottom + 5}px`; tooltipDiv.style.left = `${node.getBoundingClientRect().left}px`; document.body.appendChild(tooltipDiv); } function mouseOut() { if (tooltipDiv) { tooltipDiv.remove(); tooltipDiv = null; } } node.addEventListener('mouseover', mouseOver); node.addEventListener('mouseout', mouseOut); return { update(newText) { text = newText; }, destroy() { node.removeEventListener('mouseover', mouseOver); node.removeEventListener('mouseout', mouseOut); if (tooltipDiv) tooltipDiv.remove(); } }; } </script> <button use:tooltip="Click me!">Hover me</button>
Actions help keep your components simple by moving element-specific code outside.
Always clean up event listeners or timers in the destroy method to avoid memory leaks.
You can pass parameters to actions and update them dynamically.
Actions add reusable behavior directly to HTML elements.
They help share and organize code for element interactions.
Actions can set up, update, and clean up behavior easily.
Practice
actions in Svelte?Solution
Step 1: Understand what actions do
Actions in Svelte attach behavior to elements, like event listeners or animations.Step 2: Compare with other options
Creating components, managing state, or styling are done differently, not by actions.Final Answer:
To add reusable behavior directly to HTML elements -> Option DQuick Check:
Actions = reusable element behavior [OK]
- Confusing actions with components
- Thinking actions manage app state
- Assuming actions handle styling
tooltip on a button element in Svelte?Solution
Step 1: Recall Svelte action syntax
Actions are applied withuse:actionNameon elements.Step 2: Check each option
Onlyuse:tooltipmatches the correct syntax; others are for events or bindings.Final Answer:
<button use:tooltip>Click me</button> -> Option AQuick Check:
Action syntax = use:actionName [OK]
- Using 'action=' instead of 'use:'
- Confusing actions with event handlers
- Trying to bind actions like variables
function highlight(node) {
node.style.backgroundColor = 'yellow';
return {
destroy() {
node.style.backgroundColor = '';
}
};
}What happens when this action is used on an element?
Solution
Step 1: Analyze the action behavior
The action sets background to yellow on start, and resets it on destroy.Step 2: Understand lifecycle
Destroy runs when element is removed or action is cleaned up, resetting style.Final Answer:
The element's background turns yellow and resets when removed -> Option BQuick Check:
Action sets and cleans up style [OK]
- Thinking style never resets
- Assuming style changes only on hover
- Ignoring the destroy cleanup
function focus(node) {
node.focus();
}
<input use:focus />Solution
Step 1: Check action return value
Actions should return an object with a destroy method to clean up; here it returns nothing.Step 2: Understand optional return
Returning nothing is allowed if no cleanup needed, but best practice is to return an object.Final Answer:
The action should return an object with a destroy method -> Option CQuick Check:
Actions should return cleanup object [OK]
- Thinking actions must be async
- Believing actions need lifecycle hooks
- Assuming actions must be variables
Solution
Step 1: Understand reusable behavior with actions
Actions attach behavior directly to elements and can manage events and cleanup.Step 2: Compare options for tooltip
Only an action adding event listeners and cleanup fits reusable behavior on any element.Final Answer:
Create an action that adds event listeners to show/hide tooltip and cleans up on destroy -> Option AQuick Check:
Actions manage reusable element behavior best [OK]
- Using components instead of actions for element behavior
- Relying only on CSS without JS for dynamic tooltips
- Modifying innerHTML directly causing side effects
Ready to prove it?
Pass the test and this pattern moves unknown
