Writable stores let you keep and change data that your Svelte app can use anywhere. They help your app remember things and update the screen automatically.
Writable stores in Svelte
import { writable } from 'svelte/store'; const storeName = writable(initialValue); // To update the store: storeName.set(newValue); // To update based on current value: storeName.update(current => newValue);
writable creates a store you can read and write.
Use set to replace the value, and update to change it based on the current value.
count starting at 0.import { writable } from 'svelte/store'; const count = writable(0);
count store value to 5.count.set(5);
count value by 1.count.update(n => n + 1);This Svelte component uses a writable store count to keep track of a number. Clicking 'Increase' adds 1 to the count. Clicking 'Reset' sets it back to 0. The count value shows on the page and updates automatically.
<script> import { writable } from 'svelte/store'; const count = writable(0); function increment() { count.update(n => n + 1); } function reset() { count.set(0); } </script> <button on:click={increment} aria-label="Increase count">Increase</button> <button on:click={reset} aria-label="Reset count">Reset</button> <p>Count: {$count}</p>
Use the $ prefix to auto-subscribe to the store in your markup.
Writable stores are reactive: when the value changes, the UI updates automatically.
Always import writable from 'svelte/store' to create writable stores.
Writable stores hold data you can change and share in Svelte apps.
Use set to replace the value and update to change it based on current value.
Use the $ prefix to read the store value reactively in your components.
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
