Skip to main content
Create a Readable store that allows reading by subscription. Unlike writable stores, readable stores cannot be updated from the outside - their value can only be set by the start function.

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.
Readable<T>
object
A readable store object with the following method:
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.

Store Contract

A readable store implements the following contract:

subscribe

Subscribe to value changes:
Unlike writable stores, readable stores do not have set or update methods accessible from the outside. The value can only be changed from within the start function.

Examples

Current time store

Mouse position store

Geolocation store

Auto-subscription with $

In Svelte components, you can use the $ prefix to automatically subscribe and unsubscribe:

TypeScript