Writable store that allows both updating and reading by subscription.
Usage
Signature
Initial value of the store.
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.A writable store object with the following methods:
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.Set the store value and inform all subscribers.
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: