Consider this Svelte component using a readable store:
import { readable } from 'svelte/store';
const time = readable(new Date(), function start(set) {
const interval = setInterval(() => {
set(new Date());
}, 1000);
return () => clearInterval(interval);
});
<script>
import { time } from './timeStore';
</script>
<p>Current time: {$time.toLocaleTimeString()}</p>What will the component display?
import { readable } from 'svelte/store'; const time = readable(new Date(), function start(set) { const interval = setInterval(() => { set(new Date()); }, 1000); return () => clearInterval(interval); }); <script> import { time } from './timeStore'; </script> <p>Current time: {$time.toLocaleTimeString()}</p>
Think about what the setInterval inside the readable store does.
The readable store sets up an interval that updates the time every second by calling set(new Date()). This causes the component to re-render with the updated time every second.
Given this readable store in Svelte:
import { readable } from 'svelte/store';
const count = readable(0, (set) => {
let i = 0;
const interval = setInterval(() => {
i += 1;
set(i);
}, 1000);
return () => clearInterval(interval);
});What will be the value of $count after 3 seconds?
import { readable } from 'svelte/store'; const count = readable(0, (set) => { let i = 0; const interval = setInterval(() => { i += 1; set(i); }, 1000); return () => clearInterval(interval); });
Count how many times the interval callback runs in 3 seconds.
The interval runs every 1000ms (1 second), incrementing i and setting the store. After 3 seconds, it runs 3 times, so the store value is 3.
Which of the following code snippets correctly creates a readable store that updates every second and cleans up properly?
Remember the cleanup function must be returned and clear the interval.
Option D correctly sets up an interval and returns a cleanup function that clears it. Option D never clears the interval. Option D clears the interval immediately, so no updates happen. Option D returns the clearInterval function itself without calling it with the interval id.
Look at this Svelte readable store and component:
const data = readable('initial', (set) => {
fetch('/api/data')
.then(response => response.json())
.then(json => {
set(json.value);
});
});
<script>
import { data } from './dataStore';
</script>
<p>Data: {$data}</p>The component always shows 'initial' and never updates. Why?
const data = readable('initial', (set) => { fetch('/api/data') .then(response => response.json()) .then(json => { set(json.value); }); }); <script> import { data } from './dataStore'; </script> <p>Data: {$data}</p>
Think about when the start function in readable runs and when subscribers exist.
The start function in a readable store runs only when the first subscriber subscribes. If the store is imported but not subscribed to before fetch resolves, the set call happens before subscription, so the component never updates.
Which statement best describes the difference between a readable store and a writable store in Svelte?
Consider who can change the store's value after creation.
Readable stores expose only a subscribe method and update their value internally via the start function. Writable stores expose set and update methods allowing external code to change their value.