Skip to content

raf-query is early. The API can still change between minor releases, so use it at your own risk.

raf-query

Reference

API reference

Every export of @raf-query/react, its TanStack Router subpath and @raf-query/core, with query options and their defaults.

raf-query's API is functions, not methods: the client is a cache you pass to load and to the verbs. @raf-query/react re-exports everything from @raf-query/core, so apps import from one place.

The client

const 
const queryClient: QueryClient<{
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
queryClient
=
createQueryClient<Recommended>(config?: Omit<QueryClientConfig, "defaults" | "plugins"> & {
    readonly defaults?: NoInfer<Omit<CoreOptions, "ssr"> & Partial<{
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | false | undefined;
    }>> | undefined;
    readonly plugins?: Recommended | undefined;
}): QueryClient<{
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | ... 1 more ... | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | ... 1 more ... | undefined) => number | ... 1 more ... | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | false | undefined;
    };
}>

Create a query client. Behaviors are opt-in: pass plugins, such as recommended(). The client's root is what they declare.

createQueryClient
({
plugins?: Recommended | undefinedplugins: function recommended(): Recommended

The behaviors most apps want: retries, refetching on focus and reconnect, and polling. Pass it as the client's plugins, or spread it among your own:

recommended
(),
defaults?: NoInfer<Omit<CoreOptions, "ssr"> & Partial<{
    readonly retry: number | false | undefined;
    readonly refetchOnWindowFocus: boolean | undefined;
    readonly refetchOnReconnect: boolean | undefined;
    readonly refetchInterval: number | false | undefined;
}>> | undefined
defaults
: { staleTime?: number | undefined

How long data stays fresh, in ms. Default 0.

staleTime
: 30_000 },
});
ExportUse
createQueryClient(config?)Make a query cache. config: plugins, defaults, isServer, clock. Important defaults
recommended(), RecommendedThe plugins most apps want, as an array (and its type): [retry(), focusRefetch(), reconnectRefetch(), polling()]. Pass it as plugins, or spread it among your own
QueryClient<TTypes>The client type. createQueryClient returns one typed by its plugins; the bare QueryClient stands for any client, and load and the verbs given one take only what needs no plugin. An app names its own client ReturnType<typeof makeQueryClient>. Typing options by the client
client.subscribe(listener, { signal, replay }?)Cache events: added, updated, removed, and readers (a reader subscribed or left). replay: true first sends an added for every entry already cached, so a feature that syncs entries starts from what's there
client.isServerSet from config.isServer, else from whether window exists. Server clients don't retry, never gc, and reuse cached data. SSR and hydration
plugin<Decl>({ link?, setup?, context? }), Plugin<Decl>, Operation<Decl>One piece of a client's behavior: a link that wraps every request, a feature's setup, and context for every queryFn. Decl is an interface with the options it reads and the context it gives; plugin<Decl>() only declares options. Writing a plugin
Retry, Polling, FocusRefetch, ReconnectRefetchWhat the built-in plugins declare, to name them in a binding. Several clients
ClockThe type of config.clock: time and timers. Pass a fake one in tests. Testing

Plugins:

ExportKindUse
retry({ attempts, delay, when }?)linkRetry failed requests: 3 attempts, doubling from 1 s up to 30 s. retry per query. Retries
timeout(ms)linkFail a request with QueryTimeoutError, and abort its signal, when it takes longer than ms
focusRefetch()featureRefetch read, stale queries on window focus; refetchOnWindowFocus: false opts a query out. Window focus refetching
reconnectRefetch()featureRefetch read, stale queries when the browser comes back online; refetchOnReconnect: false opts a query out
polling()featureRefetch read queries every refetchInterval ms. Polling

Query factories

queryOptions and infiniteQueryOptions aren't exports. An app makes them with the binders, bound to its client, once, in its own raf.ts:

src/raf.ts
import {
	function bindInfiniteQueryOptions<T = NoDecl>(): InfiniteQueryOptionsBuilder<RootOf<T>, SetsOf<T>>

Makes infiniteQueryOptions, the builder of a paged query factory's options, bound like bindQueryOptions. Bind it next to queryOptions, in apps that page:

bindInfiniteQueryOptions
,
function bindQueryOptions<T = NoDecl>(): QueryOptionsBuilder<RootOf<T>, SetsOf<T>>

Makes queryOptions, the builder of a query factory's options, bound to the clients its factories run on:

bindQueryOptions
,
function bindUseQueryClient<T>(): () => QueryClient<RootOf<T>>

useQueryClient typed with a root, so verbs called with the client it returns check factories and targets against that root. T is the client's type, or the type of a function that makes it. Bind once per app:

bindUseQueryClient
,
} from "@raf-query/react"; import type {
function makeQueryClient(): QueryClient<{
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
makeQueryClient
} from "./query-client.ts";
export const
const queryOptions: QueryOptionsBuilder<{
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
queryOptions
=
bindQueryOptions<() => QueryClient<{
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>>(): QueryOptionsBuilder<...>

Makes queryOptions, the builder of a query factory's options, bound to the clients its factories run on:

bindQueryOptions
<typeof
function makeQueryClient(): QueryClient<{
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
makeQueryClient
>();
export const
const infiniteQueryOptions: InfiniteQueryOptionsBuilder<{
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
infiniteQueryOptions
=
bindInfiniteQueryOptions<() => QueryClient<{
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>>(): InfiniteQueryOptionsBuilder<...>

Makes infiniteQueryOptions, the builder of a paged query factory's options, bound like bindQueryOptions. Bind it next to queryOptions, in apps that page:

bindInfiniteQueryOptions
<typeof
function makeQueryClient(): QueryClient<{
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
makeQueryClient
>();
export const
const useQueryClient: () => QueryClient<{
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
useQueryClient
=
bindUseQueryClient<() => QueryClient<{
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>>(): () => QueryClient<...>

useQueryClient typed with a root, so verbs called with the client it returns check factories and targets against that root. T is the client's type, or the type of a function that makes it. Bind once per app:

bindUseQueryClient
<typeof
function makeQueryClient(): QueryClient<{
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
makeQueryClient
>();
ExportUse
bindQueryOptions<typeof makeQueryClient>()Makes queryOptions, which types a query factory's options, bound to the app's client: it takes the core's options and those the client's plugins declare, and its queryFns get their context. Call it once, in the app's raf.ts. Query factories, Typing options by the client
bindInfiniteQueryOptions<typeof makeQueryClient>()Makes infiniteQueryOptions, for a paged query factory, bound the same way. Infinite queries
group(name), group(parent, name), GroupBuild flat keys under a path; a group is also a target that covers every key under its path. Query keys
bindQueryOptions<Decl>(), bindQueryOptions()In a package, which can't name the app's client: a builder bound to the declarations of the plugins its factories need, or to none, for factories that set the core's options and run on any client. Binding a library to plugins
Optional<Decl>In a library's binding, bindQueryOptions<Cms & Optional<Retry>>(): a plugin whose options its queries set without needing it. Binding a library to plugins
OptionsOf<typeof makeQueryClient>The options the client's builders let a query set, for a fragment several factories share. Options several factories share
DataOf<TFactory>, InputOf<TFactory>A factory's data and input types
QueryOptions, CoreOptions, QueryFunctionContext, StreamOptionsOption types. CoreOptions is staleTime, gcTime, structuralSharing and ssr. The context has client, queryKey, signal and what the client's plugins give
InfiniteQueryOptions, InfiniteQueryFunctionContext, InfiniteDataPaged option types. The context adds pageParam and direction; the data is { pages, pageParams }
QueryFactory, QueryKeyA function of an input that returns query options, and a key: any plain array
QueryOptionsBuilder, InfiniteQueryOptionsBuilderThe types of the builders the binders return

Query options, for the queryOptions and infiniteQueryOptions an app binds:

OptionDefault
queryKeyRequired. Query keys
queryFnRequired. Query functions
staleTimeThe client's default, else 0 ms
gcTimeThe client's default, else 5 minutes; never on the server
structuralSharing (keep deeply equal parts of data)The client's default, else true
select (derive what readers get from the data)None
stream ({ initial, reduce }, for a queryFn that returns an async iterable)None. Query functions
ssr (false: only a browser runs it)true. Browser-only queries
An option a plugin of the builder's client declares, such as retry. Typing options by the clientThe client's default, else the plugin's

infiniteQueryOptions also takes initialPageParam, getNextPageParam, getPreviousPageParam, keepPages and maxPages.

Starting queries

const [
const handles: {
    readonly comments: QueryHandle<(input: {
        slug: string;
    }) => InfiniteQueryOptions<readonly ["posts", "comments", string], CommentPage, number, InfiniteData<CommentPage, number>, {
        readonly "~options": {
            readonly retry: (value: number | false | undefined) => number | false | undefined;
            readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
            readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
            readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
        };
        readonly context: {};
        readonly options: {
            ...;
        };
    }>>;
    readonly post: QueryHandle<...>;
}
handles
,
const data: LoadedData<{
    readonly comments: LoadEntry<(input: {
        slug: string;
    }) => InfiniteQueryOptions<readonly ["posts", "comments", string], CommentPage, number, InfiniteData<CommentPage, number>, {
        readonly "~options": {
            readonly retry: (value: number | false | undefined) => number | false | undefined;
            readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
            readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
            readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
        };
        readonly context: {};
        readonly options: {
            ...;
        };
    }>, "prefetch">;
    readonly post: LoadEntry<...>;
}>
data
] = await
load<{
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}, {
    ...;
}>(client: QueryClient<...>, entries: {
    ...;
} & {
    ...;
}): Promise<...>

Start a loader's queries and return a serializable handle for each. It starts every entry before it waits for any, so the order of the entries can't create a waterfall. Resolves with [handles, data] once every swr and revalidate entry has data, and rejects with the error as soon as one fails. prefetch entries never make it reject. A skipToken in place of an entry starts nothing and gets no handle.

load
(
const queryClient: QueryClient<{
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
queryClient
, {
comments: LoadEntry<(input: {
    slug: string;
}) => InfiniteQueryOptions<readonly ["posts", "comments", string], CommentPage, number, InfiniteData<CommentPage, number>, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        ...;
    };
}>, "prefetch">
comments
:
prefetch<(input: {
    slug: string;
}) => InfiniteQueryOptions<readonly ["posts", "comments", string], CommentPage, number, InfiniteData<CommentPage, number>, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        ...;
    };
}>>(factory: (input: {
    slug: string;
}) => InfiniteQueryOptions<readonly ["posts", "comments", string], CommentPage, number, InfiniteData<CommentPage, number>, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        ...;
    };
}>, ...args: LoadEntryArgs<...>): LoadEntry<...>

Start the query if the cache has no fresh data, and don't wait for it. It never makes the load reject: errors show up where a component reads the handle.

prefetch
(
function commentsQuery(input: {
    slug: string;
}): InfiniteQueryOptions<readonly ["posts", "comments", string], CommentPage, number, InfiniteData<CommentPage, number>, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        ...;
    };
}>
commentsQuery
, { slug: stringslug }),
post: LoadEntry<(input: {
    slug: string;
}) => QueryOptions<readonly ["posts", "bySlug", string], Post, Post, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>, "swr">
post
:
swr<(input: {
    slug: string;
}) => QueryOptions<readonly ["posts", "bySlug", string], Post, Post, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>>(factory: (input: {
    slug: string;
}) => QueryOptions<readonly ["posts", "bySlug", string], Post, Post, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>, ...args: LoadEntryArgs<...>): LoadEntry<...>

Use cached data even when it's stale, and wait only when the cache has none. Stale data resolves at once and refetches in the background, as HTTP's stale-while-revalidate does. The load rejects if the fetch it waits for fails.

swr
(
function postQuery(input: {
    slug: string;
}): QueryOptions<readonly ["posts", "bySlug", string], Post, Post, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
postQuery
, { slug: stringslug }),
});
ExportUse
load(client, entries)Start every entry, then resolve with [handles, data]. Rejects when an swr or revalidate entry fails. Starting queries
prefetch(factory, input, options?)Start the query if the cache has no fresh data; never waits
swr(factory, input, options?)Wait only when the cache has no data; stale data resolves at once and refetches
revalidate(factory, input, options?)Wait for fresh data; fresh cached data resolves without a refetch
skipTokenIn place of an entry: start nothing. Its handle is undefined and data has no key for it. Conditional queries
waterfallHandle(factory, input)The handle of a query whose input is only known while rendering. Request waterfalls
waterfallHandle(factory, handle, update)A handle with update(handle.input) as its input, or handle itself while that maps to the same query. URL state
QueryHandle<TFactory>{ queryKey, input }, serializable; what load returns for every entry but a skipToken. Handles
LoadEntry, Loaded, LoadEntryOptionsEntry and result types. LoadEntryOptions is { pages }: how many pages a paged factory's first load fetches

Reading queries

const [
const postThenable: QueryValue<(input: {
    slug: string;
}) => QueryOptions<readonly ["posts", "bySlug", string], Post, Post, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>>
postThenable
,
const postState: QueryState<(input: {
    slug: string;
}) => QueryOptions<readonly ["posts", "bySlug", string], Post, Post, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>>
postState
] =
useQuery<(input: {
    slug: string;
}) => QueryOptions<readonly ["posts", "bySlug", string], Post, Post, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>>(factory: (input: {
    slug: string;
}) => QueryOptions<readonly ["posts", "bySlug", string], Post, Post, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>, handle: QueryHandle<...>, options?: UseQueryOptions): readonly [...]

Read a query by its handle.

useQuery
(
const factory: (input: {
    slug: string;
}) => QueryOptions<readonly ["posts", "bySlug", string], Post, Post, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>
factory
,
const handle: QueryHandle<(input: {
    slug: string;
}) => QueryOptions<readonly ["posts", "bySlug", string], Post, Post, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>>
handle
, const options: UseQueryOptions | undefinedoptions);
const const post: Postpost = use<Post>(usable: React.Usable<Post>): Postuse(
const postThenable: QueryValue<(input: {
    slug: string;
}) => QueryOptions<readonly ["posts", "bySlug", string], Post, Post, {
    readonly "~options": {
        readonly retry: (value: number | false | undefined) => number | false | undefined;
        readonly refetchOnWindowFocus: (value: boolean | undefined) => boolean | undefined;
        readonly refetchOnReconnect: (value: boolean | undefined) => boolean | undefined;
        readonly refetchInterval: (value: number | false | undefined) => number | false | undefined;
    };
    readonly context: {};
    readonly options: {
        readonly retry: number | false | undefined;
        readonly refetchOnWindowFocus: boolean | undefined;
        readonly refetchOnReconnect: boolean | undefined;
        readonly refetchInterval: number | ... 1 more ... | undefined;
    };
}>>
postThenable
);

useQuery(factory, handle, options?) returns a Thenable<Data> for every handle, next to its state. Read it with use(), which returns at once when the data is cached and suspends otherwise. A query nothing loaded fetches while rendering in the browser; on the server it renders its Suspense fallback. It takes factories that aren't paged. The hooks belong in client components: in a server component each throws. Reading queries

Options:

OptionUse
whilePending"suspend" (the default) shows the Suspense fallback for a new input; "previous" keeps the last data
ControlMeaning
isPendingNo data yet for the handle's input; use() on the thenable will suspend
isPreviousShowing the previous handle's data while the new one loads (whilePending: "previous")
isFetchingA request is in flight: first load, new input, or background refetch
isStaleData is older than staleTime, or invalidated
errorThe last request's error while it's the latest outcome. Existing data stays readable
updatedAtWhen the data was fetched; 0 without data
queryKey, inputThe handle's key and input
hashStable string form of the handle's key, for key props on Suspense boundaries
refetch()Fetch again, or join the request in flight. Returns a promise that resolves when it settles

The state is tracked. The component re-renders when the thenable changes or settles, or when a field it read while rendering changes. The state object keeps its identity until then, and its functions keep theirs while the component is mounted. A field read only in an effect or a handler isn't tracked and holds its value from the last render. Tracked state

useInfiniteQuery(factory, handle, options?) reads a paged factory the same way, with the same options. Its state adds core's PageControls: fetchNextPage(), fetchPreviousPage(), hasNextPage, hasPreviousPage, isFetchingNextPage and isFetchingPreviousPage, and it's tracked the same way: a component that reads only the pages doesn't re-render while another page loads, and one that reads isFetchingNextPage does. Infinite queries

Read the thenable's status, value and reason to render without Suspense. See Reading without Suspense.

ExportUse
useIsFetching(target?)How many queries have a request in flight: all, or a factory's, group's or target's. 0 on the server. Background fetching indicators
QueryState, InfiniteQueryState, PageControls, QueryValue, UseQueryOptions, ThenableTypes of what useQuery and useInfiniteQuery take and return

Changing the cache

Each verb takes the client first. A factory matches the queries it built: all of them, or those whose input matches a partial input (PartialInput<TFactory>, part of the factory's input, whatever its key looks like). A group matches the keys that start with its path. A target ({ where }, such as a tag) matches the queries its where picks. Query invalidation

ExportUse
invalidateOne(client, factory, input)Mark one query stale. If it's read, it refetches; resolves when that settles
invalidateMany(client, factory, partial?) / (client, group) / (client, target, param?)Mark many queries stale; read ones refetch
invalidateMany(client, [target, [factory, partial], [target, param], …])Several targets in one call: each read query they cover refetches once, side by side; resolves when all settle
invalidateAll(client)Mark every query stale; read ones refetch
resetOne, resetMany, resetAllDrop data and errors. Read queries suspend and refetch; resolves when they settle. resetMany takes a list too
removeOne, removeMany, removeAllRemove entries and abort their requests. removeMany takes a list too
setData(client, factory, input, updater) / (client, factory, updater)Write data, as if a request had just returned it. A factory without an input takes the second form. Mutations
getData(client, factory, input)Cached data (before select), or undefined
isFetching(client, target?)How many queries have a request in flight
fetchNextPage(client, factory, input) / fetchPreviousPage(…)Load a page at either end of a paged query, outside React. Infinite queries
hasNextPage(client, factory, input) / hasPreviousPage(…)Whether a paged query has a page at that end. Infinite queries
ClientFor<TFactory>, PartialInput<TFactory>, Target, QueryInfo, GroupTypes the verbs take. ClientFor is a client that runs the factory. QueryInfo is what where sees: queryKey, input, data, options

Errors

ExportUse
QueryTimeoutErrorWhat a request fails with when timeout() cuts it off
abortError()Makes the AbortError (a DOMException) that a load waiting on a removed query rejects with

Providers

ExportUse
QueryClientProviderProvides a client to the tree, and runs its features while mounted. In every framework; see the provider
useQueryClient()Read the client, typed as any client. Verbs given it take only factories that need no plugin, so an app's components use the bound one
bindUseQueryClient<typeof makeQueryClient>()Makes useQueryClient typed with the app's client, so verbs take its factories. Call it once, in the app's raf.ts. Typing options by the client
HydrationBoundaryWrites server data into the client: a loader's state, or in server components the request's client. React Router

Hydration and devtools

Also on the main entry. An app that doesn't import them doesn't bundle them.

ExportUse
dehydrate, dehydrateStream, hydrate, canDehydrate, RedactedError, and their option and state typesMove a cache to the browser without an integration. SSR and hydration
QueryDevtools, QueryDevtoolsPanelDevtools

@raf-query/core exports the same, except the devtools, for code that doesn't use React.

TanStack Router

EntryExports
@raf-query/react/tanstack-routersetupRouterIntegration({ queryClient, router, dehydrateOptions?, handleRedirects? }). TanStack Start