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.
createVirtualStore returns a stable store handle whose backing store instance can change over time. You provide a derive function that uses the $ accessor to read from other stores and returns a store instance. Whenever the $ dependencies change, the derive function re-runs, a new backing store is created, and all existing subscriptions rebind to it transparently — without consumers needing to know the underlying store changed. This makes createVirtualStore the right tool when you need to swap an entire store (not just recompute a value) based on reactive state.
Signatures
VirtualStore<Store> is defined as:
persist? when the backing store uses persistence — so consumers interact with the virtual store exactly as they would with the concrete store.
Parameters
A derive function that reads reactive dependencies via Only read state that should actually trigger a new store creation. Accessing deeply nested values through
$ and returns a store instance. This function is re-evaluated each time a tracked dependency changes, and the returned store becomes the new backing instance. The $ accessor behaves identically to the one in createDerivedStore.$ that change frequently may cause unnecessary store reinitializations.An optional factory that receives a stable
getStore accessor and returns method overrides to attach to the virtual store’s public interface. Useful when you need to customize the public getState or setState signatures — for example, to accept an id argument that routes to a specific cache entry.Optional configuration object. See
VirtualStoreOptions below.VirtualStoreOptions
Locks the dependency graph after the first evaluation of
createStore. Subscriptions to upstream stores are established once and reused rather than being rebuilt each time the backing store is swapped.The default is true — the opposite of createDerivedStore’s default — because virtual store dependency graphs are almost always static: you read a single identifying value (e.g. a wallet address or user ID) and create a store from it.Set to false only if your createStore function reads dependencies conditionally across different evaluations.Enables console logging of dependency tracking and store-swapping events.
DeriveGetter
The $ accessor inside createStore is the same DeriveGetter type used in createDerivedStore. It supports both proxy-based and selector-based access patterns:
Return Value
The returned virtual store exposes the same interface as the backing store type, including:Returns the current state of the active backing store. Delegates directly to the backing store’s
getState.Updates state on the active backing store. If the backing store uses async storage, returns
Promise<void>; otherwise returns void.Subscribes to changes in the active backing store. When the backing store is swapped, the subscription automatically rebinds to the new instance.
Returns the initial state of the currently active backing store.
Present when the backing store is persisted. Exposes
hasHydrated, rehydrate, clearStorage, onHydrate, onFinishHydration, getOptions, setOptions, and (for async storage) hydrationPromise. The persist field is always optional on the virtual store because the backing store’s persistence configuration can vary at runtime.React Hook Usage
In the React build, the virtual store is also callable as a hook:When to Use vs. createDerivedStore
createDerivedStore | createVirtualStore | |
|---|---|---|
| Output | A computed value derived from source stores | A full store instance that changes over time |
| Reactive input | One or more state slices | An identifier or key that determines which store to use |
| Mutation | Read-only | Writes delegate to the active backing store |
| Persistence | Not applicable | Inherited from backing store |
| Typical use case | Format data, aggregate values, filter lists | Per-user / per-account / per-address store instances |
createDerivedStore when you want to compute a value. Use createVirtualStore when you want to swap the entire store backing a component tree.