Readable stores let you share data that can be read but not changed directly. They help keep your app organized and safe.
Readable stores in Svelte
import { readable } from 'svelte/store'; const store = readable(initialValue, (set) => { // setup code here // call set(newValue) to update return () => { // cleanup code here }; });
The readable function takes two arguments: the initial value and a function to set up updates.
The setup function receives a set function to update the store value and can return a cleanup function.
import { readable } from 'svelte/store'; const time = readable(new Date(), (set) => { const interval = setInterval(() => { set(new Date()); }, 1000); return () => clearInterval(interval); });
import { readable } from 'svelte/store'; const greeting = readable('Hello!', () => { // no updates, so no setup needed return () => {}; });
This Svelte component uses a readable store named count that updates every second with the current timestamp. The component shows the live timestamp on the page.
<script> import { readable } from 'svelte/store'; const count = readable(0, (set) => { const interval = setInterval(() => { set(Date.now()); }, 1000); return () => clearInterval(interval); }); </script> <main> <h1>Current timestamp:</h1> <p>{$count}</p> </main>
Readable stores cannot be changed directly by components; only the setup function can update them.
Always return a cleanup function from the setup to avoid memory leaks, especially with timers or subscriptions.
Use the $ prefix in Svelte components to subscribe and get the current value automatically.
Readable stores provide read-only reactive data in Svelte.
They update values internally but prevent external changes.
Use them for data like timers, external events, or fixed values.
Practice
readable store in Svelte?Solution
Step 1: Understand readable store purpose
A readable store provides reactive data but restricts external modifications.Step 2: Compare with writable stores
Writable stores allow external updates, readable stores do not.Final Answer:
To provide reactive data that cannot be changed from outside the store -> Option CQuick Check:
Readable store = read-only reactive data [OK]
- Confusing readable with writable stores
- Thinking readable stores allow external updates
- Assuming readable stores hold static data only
Solution
Step 1: Recall readable store syntax
Readable stores take an initial value and a start function that sets updates and cleanup.Step 2: Analyze options
import { readable } from 'svelte/store'; const time = readable(new Date(), set => { const interval = setInterval(() => set(new Date()), 1000); return () => clearInterval(interval); }); correctly uses readable with a start function that updates time every second and cleans up.Final Answer:
import { readable } from 'svelte/store'; const time = readable(new Date(), set => { const interval = setInterval(() => set(new Date()), 1000); return () => clearInterval(interval); }); -> Option AQuick Check:
Readable store needs start function for updates [OK]
- Omitting the start function for dynamic data
- Using writable instead of readable
- Passing a function without calling set inside
import { readable } from 'svelte/store';
const count = readable(0, set => {
let n = 0;
const interval = setInterval(() => {
n += 1;
set(n);
}, 1000);
return () => clearInterval(interval);
});
count.subscribe(value => console.log(value));Solution
Step 1: Understand initial value and updates
The readable store starts at 0 and increments n every second, calling set(n).Step 2: Trace values over 3 seconds
At start: 0 logged immediately. After 1s: 1, after 2s: 2, after 3s: 3 logged.Final Answer:
0, 1, 2, 3 -> Option DQuick Check:
Initial + increments each second = 0,1,2,3 [OK]
- Ignoring initial value emission
- Counting only increments without initial
- Assuming subscription logs only after first interval
import { readable } from 'svelte/store';
const data = readable(0, set => {
const interval = setInterval(() => {
set(Date.now());
}, 1000);
// Missing cleanup function
});Solution
Step 1: Check readable store start function requirements
The start function must return a cleanup function or undefined for cleanup.Step 2: Analyze given code
The code creates an interval that calls set every second but does not return any cleanup function.Final Answer:
The start function does not return a cleanup function -> Option BQuick Check:
Start function must return cleanup or undefined [OK]
- Ignoring cleanup function requirement
- Thinking setInterval is disallowed in stores
- Believing set cannot be called inside start
Solution
Step 1: Understand readable store with event listener
Readable stores need a start function to set updates and cleanup listeners.Step 2: Check options for correct event handling and cleanup
import { readable } from 'svelte/store'; const width = readable(window.innerWidth, set => { const onResize = () => set(window.innerWidth); window.addEventListener('resize', onResize); return () => window.removeEventListener('resize', onResize); }); adds resize listener, calls set on resize, and removes listener on cleanup.Final Answer:
import { readable } from 'svelte/store'; const width = readable(window.innerWidth, set => { const onResize = () => set(window.innerWidth); window.addEventListener('resize', onResize); return () => window.removeEventListener('resize', onResize); }); -> Option AQuick Check:
Readable with event listener and cleanup = import { readable } from 'svelte/store'; const width = readable(window.innerWidth, set => { const onResize = () => set(window.innerWidth); window.addEventListener('resize', onResize); return () => window.removeEventListener('resize', onResize); }); [OK]
- Using writable store when read-only is needed
- Not removing event listeners on cleanup
- Passing function instead of initial value to readable
Ready to prove it?
Pass the test and this pattern moves unknown
