Guides
Infinite queries
Paged queries with infiniteQueryOptions, "load more" buttons, infinite scroll, bidirectional lists, lists opened at an item and maxPages.
Lists that load more data onto what's already shown, such as "load more" buttons and infinite scroll, are a common UI pattern. A paged factory returns infiniteQueryOptions(...), which the app's raf.ts binds next to queryOptions, and components read it with useInfiniteQuery. Loaders and verbs take it like any query.
export function 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(input: {
slug: string;
}
input: { slug: stringslug: string }) {
return infiniteQueryOptions<readonly ["posts", "comments", string], CommentPage, number, InfiniteData<CommentPage, number>>(options: InfiniteOptions<readonly ["posts", "comments", string], CommentPage, number, InfiniteData<CommentPage, number>> & Partial<{
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | false | undefined;
}> & {
readonly queryFn: (context: InfiniteQueryFunctionContext<...>) => CommentPage | PromiseLike<...>;
}): InfiniteQueryOptions<...>
infiniteQueryOptions({
BaseOptions<readonly ["posts", "comments", string]>.queryKey: readonly ["posts", "comments", string]A plain array, usually built by a group(). Handles keep its literal type.
queryKey: comments<readonly [string]>(rest_0: string): readonly ["posts", "comments", string]comments(input: {
slug: string;
}
input.slug: stringslug),
queryFn: (context: InfiniteQueryFunctionContext<readonly ["posts", "comments", string], 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 retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>) => CommentPage | PromiseLike<...>
queryFn: (context: InfiniteQueryFunctionContext<readonly ["posts", "comments", string], 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 retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
context) =>
function listComments(input: {
cursor: number;
slug: string;
}): Promise<CommentPage>
listComments({ slug: stringslug: input: {
slug: string;
}
input.slug: stringslug, cursor: numbercursor: context: InfiniteQueryFunctionContext<readonly ["posts", "comments", string], 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 retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | ... 1 more ... | undefined;
};
}>
context.pageParam: numberpageParam }),
InfiniteOptions<readonly ["posts", "comments", string], CommentPage, number, InfiniteData<CommentPage, number>>.initialPageParam: numberinitialPageParam: 0,
InfiniteOptions<readonly ["posts", "comments", string], CommentPage, number, InfiniteData<CommentPage, number>>.getNextPageParam: PageParamFn<CommentPage, number>getNextPageParam: (lastPage: CommentPagelastPage) => lastPage: CommentPagelastPage.CommentPage.nextCursor: number | nullnextCursor,
});
}
commentsis a group: it builds every comments key.initialPageParamis the first page's param.getNextPageParam(lastPage, pages, lastPageParam)returns the next page's param, ornull/undefinedwhen there are no more pages.- The
queryFnreceives the param ascontext.pageParam, next tosignal.
Reading pages
useInfiniteQuery(factory, handle, options?) returns the same [thenable, state] tuple as useQuery, with the same options. The data is { pages, pageParams }, and the state adds paging:
function function Comments(props: {
handle: QueryHandle<typeof commentsQuery>;
}): React.JSX.Element
Comments(props: {
handle: QueryHandle<typeof commentsQuery>;
}
props: { handle: 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: {
...;
};
}>>
handle: interface QueryHandle<TFactory extends QueryFactory = (input: unknown) => { readonly queryFn: (context: never) => unknown; readonly queryKey: QueryKey; }>A small serializable reference to a query: its key and its factory's
input. It carries no data; readers get that from the cache. Handles of
different factories have different types.
QueryHandle<typeof 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> }) {
const [const pagesThenable: QueryValue<(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: {
...;
};
}>>
pagesThenable, const comments: InfiniteQueryState<(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: {
...;
};
}>>
comments] = useInfiniteQuery<(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: {
...;
};
}>, handle: QueryHandle<...>, options?: UseQueryOptions): readonly [...]
Read a paged query (infiniteQueryOptions) by its handle. The data is
{ pages, pageParams }, and its state loads more pages.
useInfiniteQuery(
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,
props: {
handle: QueryHandle<typeof commentsQuery>;
}
props.handle: 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: {
...;
};
}>>
handle,
);
const data = use<InfiniteData<CommentPage, number>>(usable: React.Usable<InfiniteData<CommentPage, number>>): InfiniteData<CommentPage, number>use(const pagesThenable: QueryValue<(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: {
...;
};
}>>
pagesThenable);
return (
<>
{const data: InfiniteData<CommentPage, number>data.InfiniteData<CommentPage, number>.pages: readonly CommentPage[]pages
.ReadonlyArray<CommentPage>.flatMap<PostComment, undefined>(callback: (this: undefined, value: CommentPage, index: number, array: CommentPage[]) => PostComment | readonly PostComment[], thisArg?: undefined): PostComment[]Calls a defined callback function on each element of an array. Then, flattens the result into
a new array.
This is identical to a map followed by flat with depth 1.
flatMap((page: CommentPagepage) => page: CommentPagepage.CommentPage.items: readonly PostComment[]items)
.Array<PostComment>.map<React.JSX.Element>(callbackfn: (value: PostComment, index: number, array: PostComment[]) => React.JSX.Element, thisArg?: any): React.JSX.Element[]Calls a defined callback function on each element of an array, and returns an array that contains the results.
map((comment: PostCommentcomment) => (
<function Comment(props: {
readonly author: string;
readonly body: string;
}): ReactNode
Comment
React.Attributes.key?: React.Key | null | undefinedkey={comment: PostCommentcomment.PostComment.id: stringid}
author: stringauthor={comment: PostCommentcomment.PostComment.author: stringauthor}
body: stringbody={comment: PostCommentcomment.PostComment.body: stringbody}
/>
))}
<React.JSX.IntrinsicElements.button: React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>button
React.ButtonHTMLAttributes<HTMLButtonElement>.disabled?: boolean | undefineddisabled={!const comments: InfiniteQueryState<(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: {
...;
};
}>>
comments.PageState.hasNextPage: booleanhasNextPage || const comments: InfiniteQueryState<(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: {
...;
};
}>>
comments.PageState.isFetchingNextPage: booleanisFetchingNextPage}
React.DOMAttributes<HTMLButtonElement>.onClick?: React.MouseEventHandler<HTMLButtonElement> | undefinedonClick={() => const comments: InfiniteQueryState<(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: {
...;
};
}>>
comments.PageControls.fetchNextPage(): Promise<void>Load the page after the last one. Resolves when it's there.
fetchNextPage()}
>
Load more
</React.JSX.IntrinsicElements.button: React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>button>
</>
);
}
| Control | Meaning |
|---|---|
fetchNextPage(), fetchPreviousPage() | Load a page at either end; resolves when it's there |
hasNextPage, hasPreviousPage | Whether the page param functions found one |
isFetchingNextPage, isFetchingPreviousPage | A page is loading at that end |
useInfiniteQuery takes only paged factories, and useQuery only factories that aren't: passing the other kind is a type error. The paging code is in useInfiniteQuery alone, so an app that never pages doesn't ship it.
Infinite scroll
Infinite scroll is the same paged query with an automatic "Load more". A ref callback watches the button with an IntersectionObserver and presses it when it nears the viewport. Unmount the button while a page loads: it watches again when it remounts, so it keeps loading while it stays in reach. Its place is taken by skeletons the size of the incoming page, so the loaded rows replace them without moving anything below.
{
const comments: InfiniteQueryState<(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: {
...;
};
}>>
comments.PageState.isFetchingNextPage: booleanisFetchingNextPage ? <function CommentSkeletons(): ReactNodeCommentSkeletons /> : null;
}
{
const comments: InfiniteQueryState<(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: {
...;
};
}>>
comments.PageState.hasNextPage: booleanhasNextPage && !const comments: InfiniteQueryState<(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: {
...;
};
}>>
comments.PageState.isFetchingNextPage: booleanisFetchingNextPage ? (
<React.JSX.IntrinsicElements.button: React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>button
React.DOMAttributes<HTMLButtonElement>.onClick?: React.MouseEventHandler<HTMLButtonElement> | undefinedonClick={() => const comments: InfiniteQueryState<(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: {
...;
};
}>>
comments.PageControls.fetchNextPage(): Promise<void>Load the page after the last one. Resolves when it's there.
fetchNextPage()}
React.RefAttributes<HTMLButtonElement>.ref?: React.Ref<HTMLButtonElement> | undefinedAllows getting a ref to the component instance.
Once the component unmounts, React will set ref.current to null
(or call the ref with null if you passed a callback ref).
ref={(button: HTMLButtonElement | nullbutton) => {
if (button: HTMLButtonElement | nullbutton === null) return;
const const observer: IntersectionObserverobserver = new var IntersectionObserver: new (callback: IntersectionObserverCallback, options?: IntersectionObserverInit) => IntersectionObserverThe IntersectionObserver interface of the Intersection Observer API provides a way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document's viewport. The ancestor element or viewport is referred to as the root.
IntersectionObserver(
(entries: IntersectionObserverEntry[]entries) => {
if (entries: IntersectionObserverEntry[]entries.Array<IntersectionObserverEntry>.some(predicate: (value: IntersectionObserverEntry, index: number, array: IntersectionObserverEntry[]) => unknown, thisArg?: any): booleanDetermines whether the specified callback function returns true for any element of an array.
some((entry: IntersectionObserverEntryentry) => entry: IntersectionObserverEntryentry.IntersectionObserverEntry.isIntersecting: booleanThe isIntersecting read-only property of the IntersectionObserverEntry interface is a Boolean value which is true if the target element intersects with the intersection observer's root.
isIntersecting)) {
const observer: IntersectionObserverobserver.IntersectionObserver.disconnect(): voidThe disconnect() method of the IntersectionObserver interface stops the observer watching all of its target elements for visibility changes.
disconnect();
void const comments: InfiniteQueryState<(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: {
...;
};
}>>
comments.PageControls.fetchNextPage(): Promise<void>Load the page after the last one. Resolves when it's there.
fetchNextPage();
}
},
{ IntersectionObserverInit.rootMargin?: string | undefinedrootMargin: "0px 0px 300px 0px" },
);
const observer: IntersectionObserverobserver.IntersectionObserver.observe(target: Element): voidThe observe() method of the IntersectionObserver interface adds an element to the set of target elements being watched by the IntersectionObserver. One observer has one set of thresholds and one root, but can watch multiple target elements for visibility changes in keeping with those.
observe(button: HTMLButtonElementbutton);
return () => const observer: IntersectionObserverobserver.IntersectionObserver.disconnect(): voidThe disconnect() method of the IntersectionObserver interface stops the observer watching all of its target elements for visibility changes.
disconnect();
}}
>
Load more
</React.JSX.IntrinsicElements.button: React.DetailedHTMLProps<React.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>button>
) : null;
}Scroll the comments below. Switch the filter to see a new list start from its first page, and switch back to find the old one still cached, with every page you loaded.
Loading several pages up front
A loader's swr and revalidate load the first page. Pass { pages: n } after the input to load more:
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: {
...;
};
}>>;
}
handles] = 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: {
...;
};
}>, "swr">
comments: swr<(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<...>
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 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 }, { LoadEntryOptions.pages?: number | undefinedFor paged factories: how many pages a first load fetches. Default 1.
pages: 2 }),
});The option only changes a first load: a query that already has pages keeps the ones it has.
Refetching
When a paged query refetches (invalidated, refetched on focus, stale in a loader), every loaded page is refetched in order, starting from the first, so cursors stay consistent: page two's param is recomputed from the new page one. A refetch stops early if getNextPageParam finds no more pages.
Pages written with setData while a request runs stay, so a live source that writes the pages it has loaded (a subscription per page) doesn't have to send them again after every load:
fetchNextPage()andfetchPreviousPage()add the new page to the data as it is when the page arrives. If a write moved the end the page was loaded for, the write wins and the page is dropped.- A refetch keeps a page written after that page's read started. A page written before its read started is read again, and the read wins.
Bidirectional lists
Add getPreviousPageParam(firstPage, pages, firstPageParam) to load pages before the first one, for a comment thread opened at a linked comment, which starts in the middle:
infiniteQueryOptions<readonly ["posts", "comments", string, "around", string], CommentWindow, string, InfiniteData<CommentWindow, string>>(options: InfiniteOptions<readonly ["posts", "comments", string, "around", string], CommentWindow, string, InfiniteData<CommentWindow, string>> & Partial<{
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | false | undefined;
}> & {
...;
}): InfiniteQueryOptions<...>
infiniteQueryOptions({
BaseOptions<readonly ["posts", "comments", string, "around", string]>.queryKey: readonly ["posts", "comments", string, "around", string]A plain array, usually built by a group(). Handles keep its literal type.
queryKey: comments<readonly [string, "around", string]>(rest_0: string, rest_1: "around", rest_2: string): readonly ["posts", "comments", string, "around", string]comments(const input: {
slug: string;
comment: string;
}
input.slug: stringslug, "around", const input: {
slug: string;
comment: string;
}
input.comment: stringcomment),
queryFn: (context: InfiniteQueryFunctionContext<readonly ["posts", "comments", string, "around", string], string, {
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;
};
}>) => CommentWindow | PromiseLike<...>
queryFn: (context: InfiniteQueryFunctionContext<readonly ["posts", "comments", string, "around", string], string, {
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;
};
}>
context) =>
function listCommentsAround(input: {
readonly cursor: string;
readonly signal: AbortSignal;
readonly slug: string;
}): Promise<CommentWindow>
listCommentsAround({
cursor: stringcursor: context: InfiniteQueryFunctionContext<readonly ["posts", "comments", string, "around", string], string, {
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;
};
}>
context.pageParam: stringpageParam,
signal: AbortSignalsignal: context: InfiniteQueryFunctionContext<readonly ["posts", "comments", string, "around", string], string, {
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;
};
}>
context.signal: AbortSignalAborted when the request is restarted or its entry is removed.
signal,
slug: stringslug: const input: {
slug: string;
comment: string;
}
input.slug: stringslug,
}),
InfiniteOptions<readonly ["posts", "comments", string, "around", string], CommentWindow, string, InfiniteData<CommentWindow, string>>.initialPageParam: stringinitialPageParam: const input: {
slug: string;
comment: string;
}
input.comment: stringcomment,
InfiniteOptions<readonly ["posts", "comments", string, "around", string], CommentWindow, string, InfiniteData<CommentWindow, string>>.getNextPageParam: PageParamFn<CommentWindow, string>getNextPageParam: (lastPage: CommentWindowlastPage) => lastPage: CommentWindowlastPage.CommentWindow.nextCursor: string | nullnextCursor,
InfiniteOptions<readonly ["posts", "comments", string, "around", string], CommentWindow, string, InfiniteData<CommentWindow, string>>.getPreviousPageParam?: PageParamFn<CommentWindow, string> | undefinedgetPreviousPageParam: (firstPage: CommentWindowfirstPage) => firstPage: CommentWindowfirstPage.CommentWindow.previousCursor: string | nullpreviousCursor,
});
context.direction tells the queryFn which way it's loading: "backward" for a page before the first, "forward" otherwise.
Opening at an item
With the linked comment in the key, every link is its own list. Following a link to a comment the page already shows then starts a new list there, and the pages the reader scrolled through are gone. To keep them, leave the comment out of the key and let keepPages say when the cached list still suits the link:
infiniteQueryOptions<readonly ["posts", "comments", string, "around"], CommentWindow, string, InfiniteData<CommentWindow, string>>(options: InfiniteOptions<readonly ["posts", "comments", string, "around"], CommentWindow, string, InfiniteData<CommentWindow, string>> & Partial<{
readonly retry: number | false | undefined;
readonly refetchOnWindowFocus: boolean | undefined;
readonly refetchOnReconnect: boolean | undefined;
readonly refetchInterval: number | false | undefined;
}> & {
...;
}): InfiniteQueryOptions<...>
infiniteQueryOptions({
// The thread, wherever it opens: one entry for every linked comment.
BaseOptions<readonly ["posts", "comments", string, "around"]>.queryKey: readonly ["posts", "comments", string, "around"]A plain array, usually built by a group(). Handles keep its literal type.
queryKey: comments<readonly [string, "around"]>(rest_0: string, rest_1: "around"): readonly ["posts", "comments", string, "around"]comments(const input: {
slug: string;
comment: string;
}
input.slug: stringslug, "around"),
queryFn: (context: InfiniteQueryFunctionContext<readonly ["posts", "comments", string, "around"], string, {
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;
};
}>) => CommentWindow | PromiseLike<...>
queryFn: (context: InfiniteQueryFunctionContext<readonly ["posts", "comments", string, "around"], string, {
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;
};
}>
context) =>
function listCommentsAround(input: {
readonly cursor: string;
readonly signal: AbortSignal;
readonly slug: string;
}): Promise<CommentWindow>
listCommentsAround({
cursor: stringcursor: context: InfiniteQueryFunctionContext<readonly ["posts", "comments", string, "around"], string, {
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;
};
}>
context.pageParam: stringpageParam,
signal: AbortSignalsignal: context: InfiniteQueryFunctionContext<readonly ["posts", "comments", string, "around"], string, {
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;
};
}>
context.signal: AbortSignalAborted when the request is restarted or its entry is removed.
signal,
slug: stringslug: const input: {
slug: string;
comment: string;
}
input.slug: stringslug,
}),
InfiniteOptions<readonly ["posts", "comments", string, "around"], CommentWindow, string, InfiniteData<CommentWindow, string>>.initialPageParam: stringinitialPageParam: const input: {
slug: string;
comment: string;
}
input.comment: stringcomment,
InfiniteOptions<readonly ["posts", "comments", string, "around"], CommentWindow, string, InfiniteData<CommentWindow, string>>.getNextPageParam: PageParamFn<CommentWindow, string>getNextPageParam: (lastPage: CommentWindowlastPage) => lastPage: CommentWindowlastPage.CommentWindow.nextCursor: string | nullnextCursor,
InfiniteOptions<readonly ["posts", "comments", string, "around"], CommentWindow, string, InfiniteData<CommentWindow, string>>.getPreviousPageParam?: PageParamFn<CommentWindow, string> | undefinedgetPreviousPageParam: (firstPage: CommentWindowfirstPage) => firstPage: CommentWindowfirstPage.CommentWindow.previousCursor: string | nullpreviousCursor,
// The thread on screen has the linked comment: keep it.
InfiniteOptions<readonly ["posts", "comments", string, "around"], CommentWindow, string, InfiniteData<CommentWindow, string>>.keepPages?: ((data: InfiniteData<CommentWindow, string>) => boolean) | undefinedWhether the cached pages suit this input. Pages grow from the
initialPageParam the entry was first loaded with; when part of the
input picks only that start and stays out of the key (the item a list
opens at), inputs that differ there share one entry. load asks
before it uses the pages: on false it drops them, and the list loads
again from this input's initialPageParam. Readers never drop pages.
Default: keep them.
keepPages: (data: InfiniteData<CommentWindow, string>data) =>
data: InfiniteData<CommentWindow, string>data.InfiniteData<CommentWindow, string>.pages: readonly CommentWindow[]pages.ReadonlyArray<CommentWindow>.some(predicate: (value: CommentWindow, index: number, array: readonly CommentWindow[]) => unknown, thisArg?: any): booleanDetermines whether the specified callback function returns true for any element of an array.
some((page: CommentWindowpage) =>
page: CommentWindowpage.CommentWindow.items: readonly PostComment[]items.ReadonlyArray<PostComment>.some(predicate: (value: PostComment, index: number, array: readonly PostComment[]) => unknown, thisArg?: any): booleanDetermines whether the specified callback function returns true for any element of an array.
some((comment: PostCommentcomment) => comment: PostCommentcomment.PostComment.id: stringid === const input: {
slug: string;
comment: string;
}
input.comment: stringcomment),
),
});Every link now shares one entry, which keeps the initialPageParam it was first loaded with: refetches and new pages grow from there. load asks keepPages(data) before it uses the cached pages. On true it uses them as they are, so moving between comments on screen, or back and forward, reads nothing. On false it drops them, and the list loads again from this link's initialPageParam. Readers never ask, and a prefetch leaves pages it would drop alone: it's a hint, so a list never starts over under someone reading it. A router that preloads links runs loaders for links to other comments too, so load the list with prefetch while preloading (in TanStack Router, when the loader's preload is true), and with swr when navigating.
Limiting pages with maxPages
Long sessions can accumulate many pages. maxPages keeps at most that many: when a new page is added, the page at the far end is dropped. Refetches then load fewer pages, too. With maxPages, define both getNextPageParam and getPreviousPageParam, so a dropped page can be loaded again when the user scrolls back.
Outside React
Scripts, tests and bindings to other frameworks page with verbs that take the client first, like the hook's controls:
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: {
...;
};
}>, "swr">
comments: swr<(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<...>
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 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 }) });
if (hasNextPage<(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: {
...;
};
}>>(client: ClientFor<...>, 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: {
...;
};
}>, input: {
...;
}): boolean
Whether a paged query has a page after its last one: false while it has
no data, or once getNextPageParam returns undefined.
hasNextPage(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, 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 })) {
await fetchNextPage<(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: {
...;
};
}>>(client: ClientFor<...>, 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: {
...;
};
}>, input: {
...;
}): Promise<void>
Load the page after the last one of a paged query, as useInfiniteQuery's
fetchNextPage() does. Resolves when the page is there, or the request
settles; does nothing without a next page.
fetchNextPage(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, 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 });
}
const const comments: InfiniteData<CommentPage, number> | undefinedcomments = getData<(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: {
...;
};
}>>(client: ClientFor<...>, 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: {
...;
};
}>, input: {
...;
}): InfiniteData<...> | undefined
A query's cached data (before select), or undefined.
getData(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, 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 });
const comments: InfiniteData<CommentPage, number> | undefinedcomments?.pages.ReadonlyArray<CommentPage>.length: number | undefinedGets the length of the array. This is a number one higher than the highest element defined in an array.
length;fetchNextPage(client, factory, input) and fetchPreviousPage(…) resolve when the page is there, or the request settles. Without a page at that end, they do nothing. An error is in the query's state, as with the hook. hasNextPage(client, factory, input) and hasPreviousPage(…) say whether there is a page at that end.
Paging lives in @raf-query/core: useInfiniteQuery's controls are core's PageControls, the same requests and the same state these verbs read.
Changing the input
Pages belong to one key. A filter or sort in the URL changes the input, and with it the key, so a new list starts from its first page. The old list stays cached under its own key until gcTime, which is why switching back is instant. See URL state and pagination.