0
0
Svelteframework~10 mins

Action parameters in Svelte - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Action parameters
Start: Element with action
Action function called
Receive parameters: node, parameters
Use parameters inside action
Return object with update and destroy
If parameters change -> call update
On element removal -> call destroy
End
The action receives the element and parameters, uses them, and returns update and destroy functions to handle changes and cleanup.
Execution Sample
Svelte
function highlight(node, { color }) {
  node.style.backgroundColor = color;
  return {
    update({ color }) { node.style.backgroundColor = color; },
    destroy() { node.style.backgroundColor = null; }
  };
}
This action sets background color on an element and updates or cleans it up when parameters change or element is removed.
Execution Table
StepActionParameters ReceivedEffect on ElementReturn Value
1Action called on element{ color: 'yellow' }Sets backgroundColor to yellowReturns update and destroy functions
2Parameters update{ color: 'lightblue' }update() sets backgroundColor to lightblueupdate function called
3Element removedN/Adestroy() clears backgroundColordestroy function called
4No more updatesN/ANo effectAction lifecycle ends
💡 Element removed, destroy called, action lifecycle ends
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
node.style.backgroundColornullyellowlightbluenullnull
parameters.colorundefinedyellowlightblueN/AN/A
Key Moments - 3 Insights
Why does the action receive the element node and parameters?
The action needs the element to modify it and parameters to know how to modify it, as shown in step 1 of the execution_table.
What happens when parameters change after the action is applied?
The update function is called with new parameters to update the element, as shown in step 2 of the execution_table.
Why is the destroy function important?
Destroy cleans up changes when the element is removed, preventing leftover styles or effects, as shown in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the backgroundColor after step 2?
Ayellow
Blightblue
Cnull
Dunchanged
💡 Hint
Check the 'Effect on Element' column at step 2 in the execution_table.
At which step is the destroy function called?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look for 'destroy() clears backgroundColor' in the execution_table.
If the parameters never change, which function is never called?
Aaction function
Bdestroy
Cupdate
Dnone
💡 Hint
See step 2 where update is called only when parameters change.
Concept Snapshot
Svelte actions receive the element node and parameters.
They modify the element using parameters.
Return update() to handle parameter changes.
Return destroy() to clean up on removal.
Update runs only if parameters change.
Destroy runs when element is removed.
Full Transcript
In Svelte, an action is a function that receives the HTML element and parameters. It can modify the element, for example by setting styles. The action returns an object with update and destroy functions. Update runs when parameters change to update the element accordingly. Destroy runs when the element is removed to clean up any changes. This lifecycle ensures the element stays in sync with parameters and cleans up properly.