Discover how stores keep your app's data perfectly in sync without headaches!
Why stores manage shared state in Svelte - The Real Reasons
Imagine you have several parts of your app showing the same data, like a shopping cart total. You try to update each part manually whenever the cart changes.
Manually updating each part is tiring and easy to forget. If you miss one, the app shows wrong info. It's like trying to keep multiple clocks in sync by hand.
Svelte stores keep the shared data in one place. When the data changes, all parts using it update automatically, so you never see old or wrong info.
let cartTotal = 0; // update cartTotal and manually update each component
import { writable } from 'svelte/store'; const cartTotal = writable(0); // components subscribe and update automatically
Stores let your app share and update data smoothly across many parts without extra work or mistakes.
Think of a music app where the play button, progress bar, and song title all update instantly when you change the song.
Manual updates cause errors and extra work.
Stores centralize shared data for easy updates.
Apps become more reliable and easier to build.
Practice
Solution
Step 1: Understand the role of stores
Svelte stores hold shared data centrally so multiple components can access it easily.Step 2: Recognize automatic updates
When the store data changes, all components using it update automatically, keeping UI in sync.Final Answer:
They keep data in one place and update all parts automatically. -> Option CQuick Check:
Shared data + auto update = A [OK]
- Thinking stores only speed up the app
- Believing stores hold data per component only
- Assuming stores freeze data after setup
Solution
Step 1: Check import syntax
The writable store is imported from 'svelte/store' using curly braces around writable.Step 2: Verify store creation
Creating a writable store uses writable(initialValue), so writable(0) is correct.Final Answer:
import { writable } from 'svelte/store'; const count = writable(0); -> Option DQuick Check:
Correct import + writable() = B [OK]
- Importing writable without braces
- Using 'store' instead of 'writable'
- Calling store() instead of writable()
import { writable } from 'svelte/store';
const count = writable(0);
count.subscribe(value => console.log(value));
count.set(5);
count.update(n => n + 1);Solution
Step 1: Understand subscription initial call
Subscribing to a writable store immediately calls the callback with the current value, so 0 is logged first.Step 2: Follow set and update calls
count.set(5) changes value to 5, logging 5. Then count.update(n => n + 1) adds 1, logging 6.Final Answer:
0, 5, 6 -> Option AQuick Check:
Subscribe logs initial + set + update values = A [OK]
- Ignoring initial subscription call
- Thinking update replaces value instead of adding
- Missing the last logged value
import { writable } from 'svelte/store';
const user = writable({ name: 'Alice' });
// In component
user.name = 'Bob';Solution
Step 1: Identify how to update store values
Writable stores require using set() or update() to change their value, not direct property assignment.Step 2: Explain why direct property change fails
Changing user.name directly modifies the object but does not notify the store, so subscribers won't update.Final Answer:
Directly changing user.name does not update the store value. -> Option BQuick Check:
Use set() or update() to change store values [OK]
- Trying to mutate store value properties directly
- Importing writable incorrectly
- Thinking const prevents store updates
Solution
Step 1: Understand shared state needs
To share and update data across components, a single source of truth is needed.Step 2: Evaluate store usage
Creating a writable store in one file and importing it everywhere ensures all components share and react to the same data.Step 3: Compare other options
Passing props only works top-down and is cumbersome for many components; local variables are isolated; separate stores cause desync.Final Answer:
Create a writable store in a separate file and import it in all components. -> Option AQuick Check:
Single shared store file = C [OK]
- Trying to sync local variables manually
- Passing props for deeply nested sharing
- Creating multiple stores causing inconsistent state
Ready to prove it?
Pass the test and this pattern moves unknown
