Skip to main content
Create a Writable store that allows both updating and reading by subscription.

Usage

Signature

value
T
Initial value of the store.
start
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.
Writable<T>
object
A writable store object with the following methods:
subscribe
(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.
set
(value: T) => void
Set the store value and inform all subscribers.
update
(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:
This is equivalent to:

TypeScript