Recall & Review
beginner
What is a readable store in Svelte?
A readable store is a special kind of store in Svelte that lets components subscribe to its value but does not allow them to change it directly. It is used to share data that should only be updated from inside the store itself.
Click to reveal answer
beginner
How do you create a readable store in Svelte?
You use the
readable function from svelte/store. It takes an initial value and a start function that sets up how the store updates its value.Click to reveal answer
intermediate
What is the purpose of the
start function in a readable store?The
start function runs when the first subscriber subscribes. It can set up timers, fetch data, or listen to events. It returns a stop function to clean up when no subscribers remain.Click to reveal answer
beginner
Can components update the value of a readable store directly?
No. Components can only subscribe to readable stores to get their values. Only the code inside the store (like the
start function) can update the value.Click to reveal answer
intermediate
Give a simple example of a readable store that updates the current time every second.
Example:<br><pre>import { readable } from 'svelte/store';
export const time = readable(new Date(), function start(set) {
const interval = setInterval(() => {
set(new Date());
}, 1000);
return () => clearInterval(interval);
});</pre>Click to reveal answer
What does the
readable function return in Svelte?✗ Incorrect
The
readable function creates a store that components can subscribe to but cannot update directly.What should the
start function in a readable store return?✗ Incorrect
The
start function returns a cleanup function that runs when the last subscriber unsubscribes.Which of these is true about readable stores?
✗ Incorrect
Readable stores restrict updates to internal code only, unlike writable stores.
How do you subscribe to a readable store in a Svelte component?
✗ Incorrect
In Svelte components, you use the $ prefix to auto-subscribe to stores.
What happens if no components subscribe to a readable store?
✗ Incorrect
When no subscribers remain, the cleanup function returned by
start runs to stop updates.Explain what a readable store is in Svelte and how it differs from a writable store.
Think about who controls the data changes.
You got /4 concepts.
Describe how you would create a readable store that updates its value over time and cleans up when no longer needed.
Focus on the start function and cleanup.
You got /4 concepts.