0
0
Svelteframework~10 mins

Writable stores in Svelte - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Writable stores
Create writable store
Subscribe to store
Read current value
Update store value
Notify subscribers
Subscribers react to new value
This flow shows how a writable store is created, subscribed to, updated, and how subscribers get notified with the new value.
Execution Sample
Svelte
import { writable } from 'svelte/store';

const count = writable(0);

count.subscribe(value => console.log('Count:', value));

count.set(1);
count.update(n => n + 1);
This code creates a writable store, subscribes to it to log changes, then sets and updates the store value.
Execution Table
StepActionStore Value BeforeStore Value AfterSubscribers NotifiedOutput
1Create writable store with initial value 0N/A0No
2Subscribe to store00YesCount: 0
3Set store value to 101YesCount: 1
4Update store value by adding 112YesCount: 2
5No more actions22No
💡 No more updates; store value remains 2 and subscribers have latest value.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
countundefined00122
Key Moments - 3 Insights
Why do subscribers get the initial value immediately after subscribing?
Because in step 2, subscribing calls the subscriber function right away with the current store value (0), as shown in the output 'Count: 0'.
What is the difference between set() and update() methods?
set() replaces the store value directly (step 3), while update() takes a function to compute the new value from the old one (step 4), as seen in the store value changes.
Why does the subscriber get notified after every change?
Because writable stores notify all subscribers whenever the value changes, triggering the subscriber callback each time (steps 3 and 4 outputs).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the store value right after subscribing at step 2?
A1
B0
Cundefined
D2
💡 Hint
Check the 'Store Value After' column at step 2 in the execution table.
At which step does the store value become 2?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Store Value After' column for each step in the execution table.
If we remove the subscribe call, what will happen to the output?
AStore value will not update
BOutput will still log all changes
CNo output will be logged
DSubscribers will be notified but no output
💡 Hint
Refer to the execution table rows where output is logged only after subscribing.
Concept Snapshot
Writable stores in Svelte:
- Created with writable(initialValue)
- Subscribe to get updates immediately and on changes
- Use set(value) to replace value
- Use update(fn) to change value based on current
- Subscribers run callback on every change
Full Transcript
Writable stores in Svelte let you hold a value that can change over time. You create one with writable and give it a starting value. When you subscribe, your function runs right away with the current value. When you call set, the store value changes and subscribers get notified. Update lets you change the value based on the old one. This way, your app reacts automatically to changes in the store.