Virtual stores solve a specific problem: you have a family of store instances — each holding the state for a different entity, user, or address — and you want to expose a single stable store reference that tracks whichever instance is currently active. When the active instance changes, every subscriber automatically rebinds to the new one with no manual rewiring. From the outside, a virtual store looks and behaves exactly like any other store.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.
When to Use a Virtual Store
Use a virtual store when the which store to read from is itself reactive state. Common scenarios include:- Per-wallet or per-account data where the active wallet changes
- Per-user profiles that are fetched and cached as separate store instances
- Any pattern where you factory-create stores keyed by a dynamic identifier
If you just want to compute a value from other stores, use a derived store instead. Virtual stores are specifically for cases where the store instance itself needs to change — not just the derived value.
Creating a Virtual Store
createVirtualStore accepts a derive function that uses the same $ accessor as derived stores. The function should return a store instance:
walletsStore.accountAddress changes, the derive function re-runs, createUserAssetsStore is called with the new address, and all active subscriptions on userAssetsStore rebind to the new backing store automatically.
How It Works
Lazy initialization
The derive function does not run until something reads from or subscribes to the virtual store. If
userAssetsStore.getState() is never called, createUserAssetsStore is never invoked.Dependency tracking
When the virtual store first activates, it subscribes to the reactive dependencies accessed via
$ — in the example above, walletsStore.accountAddress. This is the same runtime tracking mechanism used by derived stores.Instance swap
When a tracked dependency changes, the derive function re-runs and returns a new store instance. The virtual store atomically replaces the old backing store with the new one.
Method Overrides
You can customize thegetState or setState behavior of the virtual store by passing an overrides factory as the second argument. The factory receives a getStore function that returns the current backing store instance:
Persistence
Virtual stores expose thepersist property of the underlying backing store, if it has one. This means persistence methods (rehydrate, hasHydrated, clearStorage, etc.) are available on the virtual store and delegate to the current backing instance.
lockDependencies Default
options object also accepts debugMode: boolean to log dependency tracking information to the console during development.