0
0
Svelteframework~10 mins

Action with event dispatching in Svelte - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Action with event dispatching
Component mounts
Action initialized
User event triggers action
Action dispatches custom event
Component listens and handles event
Component updates state/UI
This flow shows how a Svelte action attaches to an element, listens for user events, dispatches a custom event, and the component reacts to update UI.
Execution Sample
Svelte
function myAction(node) {
  const handleClick = () => {
    node.dispatchEvent(new CustomEvent('myEvent'));
  };
  node.addEventListener('click', handleClick);
  return { destroy() { node.removeEventListener('click', handleClick); } };
}
This Svelte action listens for clicks on an element and dispatches a custom event 'myEvent' when clicked.
Execution Table
StepActionEvent Listener AddedEvent TriggeredCustom Event DispatchedComponent Reaction
1Component mounts and action attachesclick listener added to nodeNoNoNo
2User clicks elementclick listener activeYesYes: 'myEvent'Component catches 'myEvent'
3Component updates state/UI based on eventlistener still activeNoNoUI updates accordingly
4Component unmounts or action removedclick listener removedNoNoNo further reactions
💡 Action cleans up event listener when component unmounts or action is destroyed
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
nodeDOM element referenceListener attachedListener activeListener activeListener removed
handleClickFunction definedRegistered as listenerCalled on clickNot calledRemoved
Key Moments - 2 Insights
Why does the custom event 'myEvent' need to be dispatched inside the action?
Because the action controls the element's behavior and dispatching the event inside it lets the component listen and react to user interactions, as shown in execution_table step 2.
What happens if the event listener is not removed when the component unmounts?
The listener stays active causing potential memory leaks or unexpected behavior. Execution_table step 4 shows the cleanup removing the listener.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the custom event 'myEvent' dispatched?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Check the 'Custom Event Dispatched' column in execution_table
According to variable_tracker, what happens to the 'handleClick' function after step 4?
AIt is called repeatedly
BIt is removed as listener
CIt remains registered as listener
DIt dispatches more events
💡 Hint
Look at the 'handleClick' row under 'Final' column in variable_tracker
If the user never clicks the element, which steps in execution_table will occur?
ASteps 1 and 4 only
BSteps 1, 2, and 3
CAll steps
DOnly step 2
💡 Hint
Refer to 'Event Triggered' column to see when user clicks happen
Concept Snapshot
Svelte actions attach behavior to elements.
They can listen for events and dispatch custom events.
Components listen to these custom events to update UI.
Always clean up event listeners in action's destroy.
Use node.dispatchEvent(new CustomEvent('name')) inside action.
Full Transcript
In Svelte, an action is a function that attaches to a DOM element when the component mounts. This action can add event listeners to the element, such as listening for clicks. When the user clicks the element, the action dispatches a custom event using node.dispatchEvent with a CustomEvent. The component listens for this custom event and updates its state or UI accordingly. When the component unmounts or the action is removed, the event listener is cleaned up to avoid memory leaks. This flow ensures interactive behavior is encapsulated in the action and the component reacts to events cleanly.