Action parameters let you send extra information to a Svelte action. This helps customize how the action works on an element.
Action parameters in Svelte
use:actionName={parameter}The parameter can be any JavaScript value: string, number, object, etc.
Svelte calls the action's update function automatically when the parameter changes.
<div use:highlight={true}></div><button use:tooltip={{ text: 'Click me', position: 'top' }}></button><input use:focusOnMount={true}>This example shows an action called highlight that colors the background of an element. You pass a color as a parameter. When the color changes, the action updates the background color automatically.
Click the button to toggle the color between light blue and light green on the second paragraph.
<script> let color = 'lightblue'; // A simple action that changes background color based on parameter export function highlight(node, color) { node.style.backgroundColor = color || 'yellow'; return { update(newColor) { node.style.backgroundColor = newColor || 'yellow'; }, destroy() { node.style.backgroundColor = null; } }; } </script> <p use:highlight={'lightblue'}>This paragraph has a light blue background.</p> <button on:click={() => color = color === 'lightblue' ? 'lightgreen' : 'lightblue'}> Toggle Color </button> <p use:highlight={color}>This paragraph changes color when you click the button.</p>
Always return an update function from your action if you expect the parameter to change.
Remember to clean up in the destroy function to avoid side effects.
Action parameters let you customize Svelte actions with extra data.
Use use:action={parameter} syntax to pass parameters.
Actions can react to parameter changes with an update method.
Practice
use:action={parameter}?Solution
Step 1: Understand action parameters
Action parameters allow you to send extra data to a Svelte action to customize its behavior.Step 2: Identify the purpose of
This syntax passes the parameter to the action so it can use it internally.use:action={parameter}Final Answer:
To customize the behavior of the action with extra data -> Option BQuick Check:
Action parameters customize behavior = A [OK]
- Thinking parameters define components
- Confusing parameters with styling
- Assuming parameters import libraries
color with value 'red' to a Svelte action called highlight?Solution
Step 1: Recall correct parameter passing syntax
Svelte actions receive parameters usinguse:action={parameter}where parameter can be any value or object.Step 2: Identify correct object syntax for passing color
To pass an object with a keycolorand value'red', use{color: 'red'}inside curly braces.Final Answer:
use:highlight={{color: 'red'}} -> Option AQuick Check:
Pass object with color key = D [OK]
- Using parentheses instead of curly braces
- Passing string directly without braces
- Passing variable without defining it
function border(node, { color }) {
node.style.border = `2px solid ${color}`;
return {
update({ color }) {
node.style.border = `2px solid ${color}`;
}
};
}What will be the border color after this code runs?
<div use:border={{ color: 'blue' }}>Box</div>Solution
Step 1: Understand initial border style setting
The action sets the border style to '2px solid' plus the passed color, which is 'blue'.Step 2: Confirm the border color after applying the action
The div will have a blue border because the parameter color is 'blue' and the style is applied immediately.Final Answer:
A 2px solid blue border -> Option AQuick Check:
Passed color 'blue' sets border color = B [OK]
- Assuming default color instead of passed color
- Ignoring the update method
- Thinking no border is set initially
function scale(node, factor) {
node.style.transform = `scale(${factor})`;
return {
update(newFactor) {
node.style.transform = `scale(${newFactor})`;
}
};
}Which of the following will result in an invalid CSS transform value when used as
use:scale={...}?Solution
Step 1: Analyze expected parameter type
The action expects a single number parameterfactor, not an object.Step 2: Identify which option passes an object instead of a number
use:scale={{ factor: 2 }} passes an object{ factor: 2 }, which will causescale(${newFactor})to becomescale([object Object]), an invalid CSS transform value.Final Answer:
use:scale={{ factor: 2 }} -> Option CQuick Check:
Passing object instead of number causes invalid transform = C [OK]
- Passing object when number expected
- Passing string instead of number (allowed but may cause visual issues)
- Passing objects when primitive expected
tooltip that shows a message passed as a parameter and updates the message if it changes. Which of these implementations correctly handles parameter updates?Solution
Step 1: Check for update method handling parameter changes
function tooltip(node, message) { const tip = document.createElement('span'); tip.textContent = message; node.appendChild(tip); return { update(newMessage) { tip.textContent = newMessage; }, destroy() { node.removeChild(tip); } }; }defines anupdatemethod that updates the tooltip text when the parameter changes.Step 2: Verify cleanup with destroy method
This implementation also defines adestroymethod to remove the tooltip element when the action is removed, which is good practice.Final Answer:
The implementation withupdateanddestroymethods -> Option DQuick Check:
Update method handles parameter changes = A [OK]
- Not implementing update method for parameter changes
- Not cleaning up DOM elements on destroy
- Using incorrect element types or missing return object
Ready to prove it?
Pass the test and this pattern moves unknown
