What if your app could update itself everywhere the moment data changes, without you lifting a finger?
Why Writable stores in Svelte? - Purpose & Use Cases
Imagine you have a shopping list app where multiple parts of your page need to update whenever you add or remove an item. You try to manually update each part every time the list changes.
Manually updating each part is tiring and easy to forget. If you miss one place, the UI shows old data. It's like trying to keep several clocks in sync by hand -- it's slow and error-prone.
Writable stores let you keep your data in one place. When you change the store, all parts that use it update automatically. It's like having a smart clock that tells all others the correct time instantly.
let list = [];
function addItem(item) {
list.push(item);
updateUI();
updateOtherUI();
}import { writable } from 'svelte/store'; const list = writable([]); list.update(items => [...items, newItem]);
Writable stores make your app reactive and consistent by syncing data changes everywhere instantly without extra effort.
In a chat app, when you send a message, writable stores update the message list and unread count everywhere on the screen automatically.
Manually syncing data across UI parts is slow and error-prone.
Writable stores hold data centrally and update all users automatically.
This leads to simpler, more reliable, and reactive apps.
Practice
Solution
Step 1: Understand writable store role
Writable stores in Svelte are designed to hold data that can be changed and shared reactively.Step 2: Compare options
Only To hold data that can be changed and shared across components correctly describes writable stores as changeable and shareable data holders.Final Answer:
To hold data that can be changed and shared across components -> Option BQuick Check:
Writable store = changeable shared data [OK]
- Confusing writable stores with static data
- Thinking writable stores handle styling or routing
- Assuming writable stores cannot be updated
Solution
Step 1: Check import statement
The writable store must be imported from 'svelte/store' using { writable }.Step 2: Verify store creation syntax
Creating a writable store uses writable(initialValue), so import { writable } from 'svelte/store'; const count = writable(0); is correct.Final Answer:
import { writable } from 'svelte/store'; const count = writable(0); -> Option AQuick Check:
Writable store import and creation = import { writable } from 'svelte/store'; const count = writable(0); [OK]
- Using readable instead of writable
- Forgetting to import writable
- Wrong import path or syntax
import { writable } from 'svelte/store';
const count = writable(5);
count.update(n => n + 3);
console.log($count);What will be logged to the console?
Solution
Step 1: Understand store update
The update method changes the store value from 5 to 8 by adding 3.Step 2: Check usage of $count outside component
The $ prefix to access store value works only inside Svelte components, not in plain JS code like console.log.Final Answer:
Error: $count is undefined -> Option AQuick Check:
$ prefix only works inside components [OK]
- Assuming $count works outside components
- Ignoring that update changes value
- Expecting console.log to show store value directly
import { writable } from 'svelte/store';
const count = writable(0);
count.set(count + 1);Solution
Step 1: Identify what count represents
count is a writable store object, not a number, so count + 1 is invalid.Step 2: Correct way to update store
You must use count.update(n => n + 1) or count.set(value) with a number, not count + 1.Final Answer:
count + 1 is invalid because count is a store, not a number -> Option DQuick Check:
Store object + number is invalid [OK]
- Trying to add number directly to store object
- Forgetting to use update for changes
- Assuming set needs import separately
Solution
Step 1: Create writable store with initial value 10
Using writable(10) correctly sets the initial number.Step 2: Use update with condition to double if less than 50
update receives current value n and returns n*2 if n < 50, else n unchanged, matching requirement.Step 3: Check other options
const num = writable(10); num.set(n => n < 50 ? n * 2 : n); wrongly uses set with a function, which is invalid. const num = writable(10); num.update(n => n * 2); doubles unconditionally. const num = writable(10); num.set(20); sets fixed 20 ignoring condition.Final Answer:
const num = writable(10); num.update(n => n < 50 ? n * 2 : n); -> Option CQuick Check:
Use update with condition to change value [OK]
- Using set with a function instead of a value
- Not applying condition inside update
- Setting fixed value ignoring condition
Ready to prove it?
Pass the test and this pattern moves unknown
