Skip to content

InfiniteQueryFactory

For usage after instantiation, see InfiniteQuery.

Creates a ChimericInfiniteQuery backed by TanStack Query’s infinite query cache.

import { ChimericInfiniteQueryFactory } from '@chimeric/react-query';
import { QueryClient, infiniteQueryOptions } from '@tanstack/react-query';
const queryClient = new QueryClient();
const getArchivedTodos = ChimericInfiniteQueryFactory<void, Todo[], number>({
queryClient,
getInfiniteQueryOptions: () =>
infiniteQueryOptions({
queryKey: ['archived-todos'],
queryFn: ({ pageParam }) =>
fetch(`/api/archived?page=${pageParam}`).then((r) => r.json()),
initialPageParam: 0,
getNextPageParam: (lastPage, allPages) =>
lastPage.length > 0 ? allPages.length : undefined,
}),
});
// With params
const getUserPosts = ChimericInfiniteQueryFactory<{ userId: string }, Post[], number>({
queryClient,
getInfiniteQueryOptions: (params) =>
infiniteQueryOptions({
queryKey: ['user-posts', params.userId],
queryFn: ({ pageParam }) =>
fetch(`/api/users/${params.userId}/posts?page=${pageParam}`).then((r) => r.json()),
initialPageParam: 0,
getNextPageParam: (lastPage, allPages) =>
lastPage.length > 0 ? allPages.length : undefined,
}),
});
PropertyTypeDescription
queryClientQueryClientTanStack Query client instance
getInfiniteQueryOptions(params) => ReturnType<typeof infiniteQueryOptions>Returns TanStack infinite query options

Like queries, the TanStack Query integration provides .useSuspenseHook():

const { data, hasNextPage, fetchNextPage } =
getArchivedTodos.useSuspenseHook();
// data is guaranteed non-undefined

See Suspense for details.


Same config. Returns only the idiomatic callable + .prefetch().


Same config without queryClient. Returns only .useHook(), .useSuspenseHook(), and .usePrefetchHook().