Writable store that allows both updating and reading by subscription.
Usage
Signature
T
Initial value of the store.
StartStopNotifier<T>
A function that is called when the first subscriber subscribes. It receives
set and update functions and optionally returns a cleanup function that is called when the last subscriber unsubscribes.object
A writable store object with the following methods:
(run: Subscriber<T>, invalidate?: () => void) => Unsubscriber
Subscribe to value changes. The
run callback is called immediately with the current value and then called again whenever the value changes.Returns an unsubscriber function.(value: T) => void
Set the store value and inform all subscribers.
(updater: Updater<T>) => void
Update the store value using a callback function that receives the current value and returns the new value. Informs all subscribers.
Store Contract
A writable store implements the following contract:subscribe
Subscribe to value changes:set
Set the store value directly:update
Update the store value using the current value:Examples
Basic writable store
Store with start/stop logic
Auto-subscription with $
In Svelte components, you can use the$ prefix to automatically subscribe and unsubscribe: