Documentation Index
Fetch the complete documentation index at: https://mintlify.com/christianbaroni/stores/llms.txt
Use this file to discover all available pages before exploring further.
createDerivedStore produces a read-only store whose value is computed from one or more source stores. Dependency tracking is automatic: a special $ accessor records which stores (and which slices of their state) the derivation reads, then re-runs the derive function whenever any tracked value changes. Derived stores are lazy — nothing executes until the first subscriber attaches — and they self-clean when all subscribers leave, unless keepAlive is set. In the React build the returned store is also callable as a hook.
Signature
Parameters
A pure function that reads from other stores via
$ and returns the derived value. It is re-evaluated whenever a tracked dependency changes. The function should be side-effect free; use onFetched or store actions for side effects.Either an equality function or a configuration object. When a function is passed it is used as
equalityFn. See DeriveOptions below.DeriveGetter
The $ accessor passed into deriveFunction has two call signatures.
$(store) and read properties from the result. The framework automatically builds selectors from the property accesses it observes the first time the derive function runs:
$(store, selector, equalityFn?) to depend on an explicit projection:
DeriveOptions
DeriveOptions<Derived> can be passed as either a plain equality function or a configuration object.
Determines whether the derived value has changed after re-computation. When the function returns
true the store does not notify subscribers. Pass shallowEqual when the derive function returns a new object each time but with the same scalar fields.Delays re-derivation after a dependency change. Accepts a number of milliseconds or a full debounce configuration object:When a debounced store is destroyed before the delay fires, pending updates are discarded. Call
store.flushUpdates() to force a synchronous re-derive.When
true the derived store never self-destructs even when it has no subscribers. The store persists in memory and its subscriptions to source stores remain active indefinitely. Useful for permanent caches or stores that are subscribed to intermittently. Call store.destroy() to tear it down explicitly.Locks the dependency graph after the first derivation. Subscriptions are established once and reused on subsequent runs rather than being rebuilt, which eliminates subscription churn and the overhead of proxy-tracking on re-derives.Enable this when all
$ calls in your derive function are unconditional and at the top level. Conditional dependency tracking will silently miss updates when this option is on.Enables console logging of dependency tracking activity. When set to
'verbose', the subscriptions created during every re-derive are logged instead of only on the first run.Return Value
createDerivedStore returns a DerivedStore<Derived>, which extends the standard store API with two additional methods.
Returns the current derived value. If the store has not yet been activated (no subscribers), calling
getState will trigger the initial derivation.Subscribes to changes in the derived value. The subscriber receives
(nextValue, prevValue). A derived store with at least one subscriber stays active; when the last subscriber leaves the store destroys itself (unless keepAlive: true).Tears down the derived store, unsubscribes from all source stores, and releases internal resources. Derived stores clean up automatically when they have no subscribers, so explicit calls to
destroy are only needed when keepAlive: true is set.Forces a synchronous re-derive and immediately notifies subscribers. Only meaningful for debounced derived stores — calling this on a non-debounced store is a no-op.
Reads the current derived value while activating a lazy derived store before the first subscription. Used internally by the React hook integration (
useSyncExternalStore) to ensure the store is live before rendering. Equivalent to getState for most purposes, but guarantees the store is initialized.Deprecated. Throws at runtime. Derived stores do not have a fixed initial state — their value is always computed. This method exists only to satisfy the store interface for type-level compatibility.
Deprecated. Throws at runtime. Derived stores are read-only; their state is fully determined by the derive function.
React Hook Usage
In the React build,DerivedStore<Derived> is also callable as a hook: