0
0
Svelteframework~10 mins

onMount in Svelte - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - onMount
Component starts
onMount hook runs
Execute onMount callback
Update component state if needed
Component renders with updated state
Component ready on page
When a Svelte component loads, onMount runs a function once, then the component updates and shows the result.
Execution Sample
Svelte
import { onMount } from 'svelte';

let message = '';

onMount(() => {
  message = 'Hello from onMount!';
});
This code sets a message after the component loads using onMount.
Execution Table
StepActionState BeforeState AfterComponent Rendered Output
1Component starts loadingmessage = ''message = ''Displays nothing (empty)
2onMount callback runsmessage = ''message = 'Hello from onMount!'Still shows empty (render not updated yet)
3Component re-renders after state changemessage = 'Hello from onMount!'message = 'Hello from onMount!'Displays: Hello from onMount!
4No more onMount callsmessage = 'Hello from onMount!'message = 'Hello from onMount!'Displays: Hello from onMount!
💡 onMount runs once after component loads, then stops.
Variable Tracker
VariableStartAfter onMountFinal
message'' (empty string)'Hello from onMount!''Hello from onMount!'
Key Moments - 2 Insights
Why does the component first render with an empty message before showing the updated message?
Because onMount runs after the initial render (see step 2 in execution_table), so the first render shows the initial empty state.
Does onMount run more than once if the component updates?
No, onMount runs only once after the component is first added to the page (see exit_note).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'message' at Step 2?
A'' (empty string)
B'Hello from onMount!'
Cundefined
Dnull
💡 Hint
Check the 'State After' column at Step 2 in the execution_table.
At which step does the component first display the message 'Hello from onMount!'?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Component Rendered Output' column in the execution_table.
If you remove the onMount callback, what will the final message value be?
A'' (empty string)
B'Hello from onMount!'
Cnull
Dundefined
💡 Hint
Refer to variable_tracker and understand when message changes.
Concept Snapshot
onMount runs a function once after the component loads.
Use it to run code that needs the DOM ready.
It runs after the first render.
State changes inside onMount cause a re-render.
It runs only once per component instance.
Full Transcript
In Svelte, the onMount function runs after the component first appears on the page. It lets you run code that needs the page ready, like fetching data or setting state. Initially, the component renders with default values. Then onMount runs, updates state, and the component re-renders to show the new state. onMount runs only once per component load. This helps keep code organized and ensures things happen at the right time.