Skip to main content
Create a derived value store by synchronizing one or more readable stores and applying an aggregation function over their input values.

Usage

Signature

Auto-update variant

Manual-update variant

stores
Readable<any> | Array<Readable<any>>
A single store or an array of stores to derive from.
fn
Function
The derivation function. Has two variants:Auto-update: If the function has fewer than 2 parameters, the return value becomes the derived value:
Manual-update: If the function has 2 or more parameters, it receives set and update callbacks and can return a cleanup function:
initial_value
T
The initial value of the derived store before the derivation function is first called.
Readable<T>
object
A readable store containing the derived value.
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 any of the input stores change.Returns an unsubscriber function.

Examples

Derive from single store

Derive from multiple stores

Manual control with set

Use the manual-update variant when you need async operations or more control:

Delayed derived store

Chaining derived stores

Complex derivation

Auto-subscription with $

TypeScript