Query stores manage the full lifecycle of remote data: fetching, caching, background revalidation, retries, and parameter-driven refetching. They share the same store interface as base and derived stores, so they compose naturally into the rest of your state graph. When a reactive parameter changes — such as an authenticated user’s ID — the store automatically refetches. When no components are observing the store, it stays idle and does nothing.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.
Creating a Query Store
Pass a config object tocreateQueryStore with at minimum a fetcher function:
fetcher receives (params, abortController) and returns a value or Promise. The abortController is automatically wired up so in-flight fetches are aborted when parameters change or all subscribers unmount.
Reactive Parameters
Query stores become especially powerful when their parameters are reactive. Theparams config option accepts a map of parameter keys, where each value can be:
1. A static value — used as-is, never changes:
$ accessor as derived stores:
authStore.userId changes, accountStore automatically refetches for the new user.
3. A queryParam wrapper — for cases where the fetcher needs a full value but the cache key should be derived differently:
Query Store State Interface
Every query store’s state includes the following fields and methods:| Member | Description |
|---|---|
getData(paramsOrKey?) | Returns cached data for the current (or specified) params, or null |
getStatus(key?) | Returns the full QueryStatusInfo object, or a single boolean by key |
fetch(params?, options?) | Imperatively trigger a fetch; returns Promise<TData | null> |
reset(resetStoreState?) | Tears down param subscriptions/timers; optionally resets state |
getCacheEntry(paramsOrKey?) | Low-level access to the raw cache entry |
isStale(override?) | Whether current data has exceeded staleTime |
isDataExpired(override?) | Whether current data has exceeded cacheTime |
status | Current QueryStatus string: 'idle' | 'loading' | 'success' | 'error' |
error | The most recent fetch error, or null |
queryKey | The deterministically generated key for current params |
queryCache | Raw cache map keyed by query key |
lastFetchedAt | Timestamp of last successful fetch, or null |
enabled | Whether the store is actively fetching |
Query Status
Thestatus field uses values from the QueryStatuses constant:
QueryStatusInfo
getStatus() returns a QueryStatusInfo object with pre-computed boolean flags:
| Flag | Description |
|---|---|
isIdle | No request in progress, no data, no error |
isLoading | A fetch is currently in progress |
isSuccess | Most recent fetch succeeded |
isError | Most recent fetch encountered an error |
isInitialLoad | isLoading is true and no data has been fetched yet |
Using in React and Outside React
- React
- Vanilla / Outside React
In the React build, query stores are callable hooks. Select the piece of state you need — the component re-renders only when that value changes:The store fetches automatically when the component mounts (if data is stale or absent) and cleans up when it unmounts.
Key Config Options
Use the
time utility from @storesjs/stores to define staleTime, cacheTime, and other duration values in human-readable units: time.minutes(2), time.hours(1), time.days(7). All time methods return a plain millisecond number, so they are fully compatible with any numeric duration field.FetchOptions
When calling fetch() imperatively, FetchOptions gives you fine-grained control:
Custom State with a State Creator
Pass a state creator as the second argument to extend the query store with your own local state:getData, status, fetch, etc.).
Automatic Fetch Deduplication
If multiple components subscribe to the same query store simultaneously, only one fetch request is made. Concurrent callers offetch() with matching params receive the same in-flight promise — no duplicate network requests are issued.
Query Key Utilities
createQueryStore re-exports two helpers for working with the deterministically generated query keys that power the cache:
queryCache, or construct a queryKey to pass to getData(key) or getCacheEntry(key) without having the original params object on hand.