0
0
Svelteframework~10 mins

Auto-subscription with $ prefix in Svelte - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Auto-subscription with $ prefix
Declare store
Use $ prefix in component
Svelte auto-subscribes
Component re-renders on store change
Display updated value
This flow shows how Svelte auto-subscribes to a store when you use the $ prefix, updating the component automatically when the store changes.
Execution Sample
Svelte
import { writable } from 'svelte/store';
const count = writable(0);

// In component:
// <p>{$count}</p>
// count.set(1);
This code creates a writable store and uses the $ prefix to auto-subscribe and display its value in a Svelte component.
Execution Table
StepActionStore ValueComponent $count ValueComponent Rendered Output
1Initialize count store with 000<p>0</p>
2Component uses $count to auto-subscribe00<p>0</p>
3Call count.set(1)11<p>1</p>
4Call count.set(5)55<p>5</p>
5No further changes55<p>5</p>
💡 No more store updates; component stays rendered with last value.
Variable Tracker
VariableStartAfter 1After 2After 3Final
count (store value)00155
$count (auto-subscribed value)00155
Key Moments - 2 Insights
Why does the component update automatically when count changes?
Because using $count tells Svelte to auto-subscribe to the store, so it re-renders the component whenever the store value changes, as shown in steps 3 and 4 of the execution_table.
What happens if we use count without the $ prefix in the component?
Without $, the component does not subscribe to the store and won't update automatically. The execution_table shows $count changing and triggering re-renders, which won't happen without $.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What is the component's rendered output?
A<p>1</p>
B<p>0</p>
C<p>5</p>
D<p>undefined</p>
💡 Hint
Check the 'Component Rendered Output' column at step 3 in the execution_table.
At which step does the store value first change from its initial value?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Store Value' column in the execution_table to see when it changes from 0.
If we remove the $ prefix in the component, how would the 'Component Rendered Output' change after step 3?
AIt would show an error
BIt would still update to <p>1</p>
CIt would stay at <p>0</p> and not update
DIt would display <p>undefined</p>
💡 Hint
Refer to the key_moments section explaining the role of $ prefix for auto-subscription.
Concept Snapshot
Auto-subscription with $ prefix in Svelte:
- Declare a store (e.g., writable(0))
- Use $storeName in component to auto-subscribe
- Component re-renders automatically on store changes
- No manual subscription or unsubscribe needed
- Simplifies reactive UI updates
Full Transcript
In Svelte, when you create a store like 'count' using writable, you can use the $ prefix inside your component to auto-subscribe to that store. This means the component automatically listens for changes in the store's value. When you update the store using count.set(newValue), the component sees the new value through $count and re-renders to show the updated value. This process removes the need to manually subscribe or unsubscribe from the store, making your code cleaner and reactive. The execution table shows the store starting at 0, then changing to 1 and 5, with the component updating its displayed value accordingly.