Svelte • Reference

svelte/store

On this page
import {
	derived,
	fromStore,
	get,
	readable,
	Readable,
	readonly,
	StartStopNotifier,
	Subscriber,
	toStore,
	Unsubscriber,
	Updater,
	writable,
	Writable
} from 'svelte/store';

Subscriber

Callback to inform of a value updates.

export type type Subscriber<T> = (value: T) => voidtype Subscriber<T> = (value: T) => voidSubscriber<function (type parameter) T in type Subscriber<T>function (type parameter) T in type Subscriber<T>T> = (value: Tvalue: Tvalue: function (type parameter) T in type Subscriber<T>function (type parameter) T in type Subscriber<T>T) => void;

Unsubscriber

Unsubscribes from value updates.

export type type Unsubscriber = () => voidtype Unsubscriber = () => voidUnsubscriber = () => void;

Updater

Callback to update a value.

export type type Updater<T> = (value: T) => Ttype Updater<T> = (value: T) => TUpdater<function (type parameter) T in type Updater<T>function (type parameter) T in type Updater<T>T> = (value: Tvalue: Tvalue: function (type parameter) T in type Updater<T>function (type parameter) T in type Updater<T>T) => function (type parameter) T in type Updater<T>function (type parameter) T in type Updater<T>T;

StartStopNotifier

Start and stop notification callbacks. This function is called when the first subscriber subscribes.

export type StartStopNotifier<T> = (
		set: (value: T) => void,
		update: (fn: Updater<T>) => void
	) => void | (() => void);

Readable

Readable interface for subscribing.

export interface interface Readable<T>interface Readable<T>Readable<function (type parameter) T in Readable<T>function (type parameter) T in Readable<T>T> {/*…*/}
subscribe(this: void, run: Subscriber<T>, invalidate?: () => void): Unsubscriber;

Subscribe on value changes.

Writable

Writable interface for both updating and subscribing.

export interface Writable<T> extends Readable<T> {/*…*/}
set(this: void, value: T): void;

Set value and inform subscribers.

update(this: void, updater: Updater<T>): void;

Update value using callback and inform subscribers.

toStore

export function toStore<V>(get: () => V, set: (v: V) => void): Writable<V>;
export function toStore<V>(get: () => V): Readable<V>;

fromStore

export function fromStore<V>(store: Writable<V>): {
		current: V;
	};
export function fromStore<V>(store: Readable<V>): {
		readonly current: V;
	};

readable

Creates a Readable store that allows reading by subscription.

export function readable<T>(value?: T | undefined, start?: StartStopNotifier<T> | undefined): Readable<T>;

writable

Create a Writable store that allows both updating and reading by subscription.

export function writable<T>(value?: T | undefined, start?: StartStopNotifier<T> | undefined): Writable<T>;

derived

Derived value store by synchronizing one or more readable stores and applying an aggregation function over its input values.

export function derived<S extends Stores, T>(stores: S, fn: (values: StoresValues<S>, set: (value: T) => void, update: (fn: Updater<T>) => void) => Unsubscriber | void, initial_value?: T | undefined): Readable<T>;
export function derived<S extends Stores, T>(stores: S, fn: (values: StoresValues<S>) => T, initial_value?: T | undefined): Readable<T>;

readonly

Takes a store and returns a new one derived from the old one that is readable.

export function readonly<T>(store: Readable<T>): Readable<T>;

get

Get the current value from a store by subscribing and immediately unsubscribing.

export function get<T>(store: Readable<T>): T;