Consider a Svelte store shared by two components. When the store value changes, what is the expected behavior?
import { writable } from 'svelte/store'; const count = writable(0); // Component A and Component B both subscribe to 'count' // When count.set(5) is called, what happens?
Think about how reactive subscriptions work in Svelte stores.
Svelte stores notify all subscribers immediately when their value changes. This means all components using the store update automatically.
Why is a Svelte store preferred for managing state shared across many components instead of passing props down multiple levels?
Think about how data flows in component trees and how stores simplify that.
Stores provide a centralized place for shared state, avoiding the need to pass props through many layers, which can get complicated and error-prone.
Choose the correct syntax to create a readable store that starts a timer and updates every second.
Remember the readable store's start function must return a cleanup function.
Option D correctly uses readable with a start function that sets an interval and returns a cleanup function to clear it. Option D uses writable incorrectly. Options A and D miss returning the cleanup function.
Given this code, why does the component not update when the store changes?
import { writable } from 'svelte/store'; const count = writable(0); // In component: let value; count.subscribe(val => { value = val; }); // count.set(1) is called elsewhere
Think about how Svelte tracks reactive variables and updates the UI.
Directly assigning to a normal variable inside subscribe does not trigger Svelte's reactivity. Using the '$' prefix with the store or reactive declarations is needed for automatic updates.
Given the following Svelte store and component code, what is the final value displayed?
import { writable } from 'svelte/store'; const store = writable(0); store.update(n => n + 1); store.set(5); store.update(n => n * 2); let value; store.subscribe(v => value = v); // What is the value of 'value' after these operations?
Trace each store operation step by step.
Starting at 0, update adds 1 → 1, set to 5 → 5, update multiplies by 2 → 10. The final subscribed value is 10.