Open History to see the last commit
1---
2id: useInfiniteQuery
3title: useInfiniteQuery
4---
5β
6```tsx
7const {
8 fetchNextPage,
9 fetchPreviousPage,
10 hasNextPage,
11 hasPreviousPage,
12 isFetchingNextPage,
13 isFetchingPreviousPage,
14 ...result
15} = useInfiniteQuery({
16 queryKey,
17 queryFn: ({ pageParam = 1 }) => fetchPage(pageParam),
18 ...options,
19 getNextPageParam: (lastPage, allPages) => lastPage.nextCursor,
20 getPreviousPageParam: (firstPage, allPages) => firstPage.prevCursor,
21})
22```
23β
24**Options**
25β
26The options for `useInfiniteQuery` are identical to the [`useQuery` hook](../reference/useQuery) with the addition of the following:
27β
28- `queryFn: (context: QueryFunctionContext) => Promise<TData>`
29 - **Required, but only if no default query function has been defined** [`defaultQueryFn`](../guides/default-query-function)
30 - The function that the query will use to request data.
31 - Receives a [QueryFunctionContext](../guides/query-functions#queryfunctioncontext)
32 - Must return a promise that will either resolve data or throw an error.
33 - Make sure you return the data *and* the `pageParam` if needed for use in the props below.
34- `getNextPageParam: (lastPage, allPages) => unknown | undefined`
35 - When new data is received for this query, this function receives both the last page of the infinite list of data and the full array of all pages.
36 - It should return a **single variable** that will be passed as the last optional parameter to your query function.
37 - Return `undefined` to indicate there is no next page available.
38- `getPreviousPageParam: (firstPage, allPages) => unknown | undefined`
39 - When new data is received for this query, this function receives both the first page of the infinite list of data and the full array of all pages.
40 - It should return a **single variable** that will be passed as the last optional parameter to your query function.
41 - Return `undefined` to indicate there is no previous page available.
42- `maxPages: number | undefined`
43 - The maximum number of pages to store in the infinite query data.
44 - When the maximum number of pages is reached, fetching a new page will result in the removal of either the first or last page from the pages array, depending on the specified direction.
45 - If `undefined` or equals `0`, the number of pages is unlimited
46 - Default value is `undefined`
47 - `getNextPageParam` and `getPreviousPageParam` must be properly defined if `maxPages` value is greater than `0` to allow fetching a page in both directions when needed.
48β
49**Returns**
50β
51The returned properties for `useInfiniteQuery` are identical to the [`useQuery` hook](../reference/useQuery), with the addition of the following and a small difference in `isRefetching`:
52β
53- `data.pages: TData[]`
54 - Array containing all pages.
55- `data.pageParams: unknown[]`
56 - Array containing all page params.
57- `isFetchingNextPage: boolean`
58 - Will be `true` while fetching the next page with `fetchNextPage`.
59- `isFetchingPreviousPage: boolean`
60 - Will be `true` while fetching the previous page with `fetchPreviousPage`.
61- `fetchNextPage: (options?: FetchNextPageOptions) => Promise<UseInfiniteQueryResult>`
62 - This function allows you to fetch the next "page" of results.
63 - `options.pageParam: unknown` allows you to manually specify a page param instead of using `getNextPageParam`.
64 - `options.cancelRefetch: boolean` if set to `true`, calling `fetchNextPage` repeatedly will invoke `fetchPage` every time, whether the previous
65 invocation has resolved or not. Also, the result from previous invocations will be ignored. If set to `false`, calling `fetchNextPage`
66 repeatedly won't have any effect until the first invocation has resolved. Default is `true`.
67- `fetchPreviousPage: (options?: FetchPreviousPageOptions) => Promise<UseInfiniteQueryResult>`
68 - This function allows you to fetch the previous "page" of results.
69 - `options.pageParam: unknown` allows you to manually specify a page param instead of using `getPreviousPageParam`.
70 - `options.cancelRefetch: boolean` same as for `fetchNextPage`.
71- `hasNextPage: boolean`
72 - This will be `true` if there is a next page to be fetched (known via the `getNextPageParam` option).
73- `hasPreviousPage: boolean`
74 - This will be `true` if there is a previous page to be fetched (known via the `getPreviousPageParam` option).
75- `isRefetching: boolean`
76 - Is `true` whenever a background refetch is in-flight, which _does not_ include initial `pending` or fetching of next or previous page
77 - Is the same as `isFetching && !isPending && !isFetchingNextPage && !isFetchingPreviousPage`