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.
createQueryStore creates a store that manages the full lifecycle of remote data: fetching, caching, staleness, error handling, and retry. Parameters passed to the store’s fetcher function can be reactive — derived from other stores via the $ accessor — so that changes to upstream state automatically trigger a re-fetch. The resulting store exposes a consistent query state shape through .getState(), and in the React build is also callable as a hook. Query stores integrate naturally with createBaseStore persistence and accept optional custom state to co-locate related non-query state.
Signatures
TQueryFnData— the raw type returned byfetcherTParams— the shape of the parameters object passed tofetcher(defaults toRecord<string, never>for parameter-free stores)TData— the type aftertransformis applied (defaults toTQueryFnData)
Config (QueryStoreConfig)
The first argument to createQueryStore is the query configuration object. All fetch behavior is controlled here.
fetcher
(params: TParams, abortController: AbortController | null) => TQueryFnData | Promise<TQueryFnData>
required
The function that performs the actual data fetch. Receives the resolved params and, by default, an
AbortController whose signal you can pass to fetch or Axios. The abort controller is null when abortInterruptedFetches is false or when the fetch is triggered with skipStoreUpdates: true.Reactive or static parameters passed to
fetcher. Each key can be:- A static value —
{ userId: 42 } - A reactive function
($, store) => value— re-evaluated when its$dependencies change - A
queryParam(value, { key })wrapper — separates the fetch value from the key value used to generate the query key
The duration in milliseconds that fetched data is considered fresh. Once stale, the store will refetch in the background if there are active subscribers. Accepts a static number or a reactive function
($, store) => number.The maximum duration in milliseconds that data remains in
queryCache before being eligible for pruning. Accepts a static number or a function of the current params.Controls whether the store actively fetches. When
false, no automatic fetching occurs until explicitly re-enabled. Accepts a static boolean or a reactive function ($, store) => boolean.The maximum number of retry attempts after a fetch failure before the store transitions to
'error' status.The delay between retry attempts in milliseconds. Accepts a fixed number or a function receiving the current retry count and the error. Defaults to exponential back-off starting at 5 seconds, doubling each retry, capped at 5 minutes:
Transforms the raw data returned by
fetcher into the TData type stored in the cache. Required when TData and TQueryFnData differ. Receives both the raw data and the params that produced it.A callback invoked after each successful fetch. Receives
{ data, params, fetch, set }, where set is the store’s setState function, allowing side effects such as writing related state.A callback invoked whenever a fetch operation fails. Receives the error and the current retry count for the failing operation.
Overrides the default behavior of writing fetched data into
queryCache. When provided you are fully responsible for storing and keying the data. Receives { data, params, queryKey, set }. Automatic refetch scheduling continues to operate based on staleTime, but no data is written to the internal cache by the store itself.When
true, getData() returns the most recently cached data for any key until fresh data for the current params is available. This prevents a flash of empty state during parameter transitions.Delays triggering a fetch when parameters change. Accepts
false (no throttle), a number of milliseconds, or a full debounce configuration:When
true, the store will not automatically refetch data when it becomes stale while a component is already mounted. Stale data will still trigger a refetch on the next component mount.When
true, the store skips all internal caching. Every fetch is treated as if no cached data exists. Fetched data is not stored unless a custom setData function is provided.When
true, any in-flight fetch is aborted via AbortController when a new fetch is initiated due to a param change, or when all subscribed components unmount.Enables verbose console logging of fetch lifecycle events, cache hits/misses, and param changes.
Suppresses the console warning emitted when
staleTime is set below the recommended minimum.State Shape (QueryStoreState)
The store’s .getState() returns a QueryStoreState<TData, TParams, CustomState> object. All of the following methods and properties are available on the state.
Methods
Returns cached data for the given params or query key. When called with no argument, returns data for the store’s current
queryKey. Returns null when no cached data is available.getStatus
(statusKey?: keyof QueryStatusInfo) => QueryStatusInfo | QueryStatusInfo[keyof QueryStatusInfo]
Returns status information for the current query. Pass a specific key to read a single boolean rather than building the full object:
QueryStatusInfo shape:Manually triggers a fetch. If no
params are passed, uses the store’s current parameters. Resolves with the fetched (and transformed) data, or null if the fetch fails or is skipped. See FetchOptions below.Returns the raw cache entry for the specified params or query key, or for the current params when called with no argument. Returns
null when no entry exists.Returns
true if the time elapsed since lastFetchedAt exceeds staleTime. Pass override (in ms) to check against a custom threshold instead of the configured staleTime.Returns
true if the time elapsed since lastFetchedAt exceeds cacheTime. Pass override (in ms) to check against a custom threshold.Tears down param subscriptions and clears pending timers and retry state. When
resetStoreState is true, also resets the in-memory store state (including queryCache, status, and error) back to initial values.Properties
The current status of the most recent fetch operation. Use the
QueryStatuses constant for comparison:The error thrown by the most recent failed fetch, or
null when the last fetch succeeded or no fetch has occurred.The current query key generated deterministically from the active params. Used as the key into
queryCache.The full cache of all fetched entries, keyed by their query key strings.
Unix timestamp (ms) of the most recent successful fetch, or
null if no successful fetch has occurred.Whether the store is currently configured to fetch. Reflects the resolved value of the
enabled config option.FetchOptions
Optional second argument to store.getState().fetch(params?, options?).
When
true, bypasses the stale check and always initiates a network request, even if fresh data is available. Note: if a pending fetch already matches the same params, the existing promise is returned rather than starting a duplicate request.When
true, the fetch runs in parallel and its result does not affect the store’s state. The AbortController is not provided. Use 'withCache' to run the fetch silently but still write the result to queryCache.Overrides
config.staleTime for this individual fetch call to determine whether cached data is fresh enough to return without re-fetching.Overrides
config.cacheTime for the data fetched in this operation, controlling when it becomes eligible for pruning. Has no effect when skipStoreUpdates: true.When
true, the returned promise rejects on fetch failure instead of resolving to null.Controls whether the store’s
queryKey is updated to reflect the params used for this fetch. Defaults to false for manual fetches. Useful when you want to pre-warm the cache for a different set of params without changing where getData() currently points.QueryStatuses
A const object of status string literals for type-safe comparisons:
Re-exported Utilities
ThecreateQueryStore module re-exports the following utilities for working with query keys and parameters.
Generates a deterministic query key string from a key payload object, consistent with the keys the store generates internally. Keys are sorted before serialization so that
{ b: 2, a: 1 } and { a: 1, b: 2 } produce the same string.Parses a query key string back into the key payload object it encodes. The inverse of
getQueryKey.Wraps a query parameter when its full fetcher value should be excluded from the query key or represented by a smaller key value. Pass
key: false to exclude the param from the key entirely, or key: fn to use a derived representation.defaultRetryDelay
(retryCount: number, options?: { baseDelay?: number; maxDelay?: number }) => number
The default retry delay function used when
retryDelay is not configured. Implements exponential back-off starting at baseDelay (default 5s), doubling each retry, capped at maxDelay (default 5m). Can be imported and called directly when you need to extend the default behavior.Reactive Params
Each key inparams accepts one of three forms: