0
0
Svelteframework~5 mins

Readable stores in Svelte - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AA store that components can read and write to
BA store that components can only read but not write to
CA function to update the store value
DA component that displays data
What should the start function in a readable store return?
AA cleanup function to stop updates
BThe initial value of the store
CA boolean indicating if the store is active
DNothing, it should not return anything
Which of these is true about readable stores?
AThey cannot be subscribed to
BThey allow external code to update their value
CThey only allow internal code to update their value
DThey are the same as writable stores
How do you subscribe to a readable store in a Svelte component?
AUsing the $ prefix like <code>$storeName</code>
BBy calling <code>storeName.set()</code>
CBy importing <code>subscribe()</code> from Svelte
DBy using <code>storeName.update()</code>
What happens if no components subscribe to a readable store?
AThe store resets to its initial value
BThe store continues updating regardless
CThe store throws an error
DThe store stops updating and cleans up resources
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.