Skip to content

InfiniteQuery

A ChimericInfiniteQuery represents a paginated read operation. Once instantiated via a React Query or RTK Query factory, usage is identical regardless of the backing library.

Call the infinite query as an async function:

// Without params
const { pages, pageParams } = await getArchivedTodos();
// With params
const { pages, pageParams } = await getArchivedTodos({ userId: '123' });
// Force a fresh fetch
const result = await getArchivedTodos({ options: { forceRefetch: true } });
// Fetch a specific page
const result = await getArchivedTodos({ options: { pageParam: 5 } });

Return value: { pages: TPageData[]; pageParams: TPageParam[] }

Idiomatic options:

OptionTypeDescription
options.forceRefetchbooleanBypass cache staleness
options.pageParamTPageParamOverride the initial page param
nativeOptionslibrary-specificPassed directly to the underlying library
await getArchivedTodos.prefetch();
await getArchivedTodos.prefetch({ userId: '123' });

Use .useHook() inside a React component:

function ArchivedTodoList() {
const {
data,
isPending,
isError,
error,
hasNextPage,
fetchNextPage,
isFetchingNextPage,
refetch,
native,
} = getArchivedTodos.useHook();
if (isPending) return <div>Loading...</div>;
if (isError) return <div>Error: {error?.message}</div>;
return (
<div>
{data?.pages.flat().map((todo) => (
<div key={todo.id}>{todo.title}</div>
))}
{hasNextPage && (
<button onClick={() => fetchNextPage()} disabled={isFetchingNextPage}>
{isFetchingNextPage ? 'Loading more...' : 'Load More'}
</button>
)}
</div>
);
}

Return value (in addition to standard query fields):

PropertyTypeDescription
data{ pages: TPageData[]; pageParams: TPageParam[] } | undefinedAll fetched pages
hasNextPagebooleanMore pages available forward
hasPreviousPagebooleanMore pages available backward
fetchNextPage() => Promise<...>Fetch the next page
fetchPreviousPage() => Promise<...>Fetch the previous page
isFetchingNextPagebooleanCurrently fetching next page
isFetchingPreviousPagebooleanCurrently fetching previous page
refetch() => Promise<...>Refetch all pages
nativelibrary-specificFull result from the underlying library

Reactive options:

OptionTypeDescription
options.enabledbooleanConditionally enable the query (default: true)
nativeOptionslibrary-specificPassed directly to the underlying library
getArchivedTodos.usePrefetchHook({ userId: nextUserId });

See also: Suspense for useSuspenseHook.