Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

useInfiniteQuery.md

81 lines4.5 KB

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