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

useInfiniteQuery.md

66 lines3.6 KB

1---
2id: useInfiniteQuery
3title: useInfiniteQuery
4---
5​
6```js
7const {
8 fetchNextPage,
9 fetchPreviousPage,
10 hasNextPage,
11 hasPreviousPage,
12 isFetchingNextPage,
13 isFetchingPreviousPage,
14 ...result
15} = useInfiniteQuery(queryKey, ({ pageParam = 1 }) => fetchPage(pageParam), {
16 ...options,
17 getNextPageParam: (lastPage, allPages) => lastPage.nextCursor,
18 getPreviousPageParam: (firstPage, allPages) => firstPage.prevCursor,
19})
20```
21​
22**Options**
23​
24The options for `useInfiniteQuery` are identical to the [`useQuery` hook](/reference/useQuery) with the addition of the following:
25​
26- `queryFn: (context: QueryFunctionContext) => Promise<TData>`
27 - **Required, but only if no default query function has been defined** [`defaultQueryFn`](/guides/default-query-function)
28 - The function that the query will use to request data.
29 - Receives a [QueryFunctionContext](../guides/query-functions#queryfunctioncontext)
30 - Must return a promise that will either resolve data or throw an error.
31 - Make sure you return the data *and* the `pageParam` if needed for use in the props below.
32- `getNextPageParam: (lastPage, allPages) => unknown | undefined`
33 - 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.
34 - It should return a **single variable** that will be passed as the last optional parameter to your query function.
35 - Return `undefined` to indicate there is no next page available.
36- `getPreviousPageParam: (firstPage, allPages) => unknown | undefined`
37 - 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.
38 - It should return a **single variable** that will be passed as the last optional parameter to your query function.
39 - Return `undefined` to indicate there is no previous page available.
40​
41**Returns**
42​
43The returned properties for `useInfiniteQuery` are identical to the [`useQuery` hook](/reference/useQuery), with the addition of the following:
44​
45- `data.pages: TData[]`
46 - Array containing all pages.
47- `data.pageParams: unknown[]`
48 - Array containing all page params.
49- `isFetchingNextPage: boolean`
50 - Will be `true` while fetching the next page with `fetchNextPage`.
51- `isFetchingPreviousPage: boolean`
52 - Will be `true` while fetching the previous page with `fetchPreviousPage`.
53- `fetchNextPage: (options?: FetchNextPageOptions) => Promise<UseInfiniteQueryResult>`
54 - This function allows you to fetch the next "page" of results.
55 - `options.pageParam: unknown` allows you to manually specify a page param instead of using `getNextPageParam`.
56 - `options.cancelRefetch: boolean` if set to `true`, calling `fetchNextPage` repeatedly will invoke `fetchPage` every time, whether the previous
57 invocation has resolved or not. Also, the result from previous invocations will be ignored. If set to `false`, calling `fetchNextPage`
58 repeatedly won't have any effect until the first invocation has resolved. Default is `true`.
59- `fetchPreviousPage: (options?: FetchPreviousPageOptions) => Promise<UseInfiniteQueryResult>`
60 - This function allows you to fetch the previous "page" of results.
61 - `options.pageParam: unknown` allows you to manually specify a page param instead of using `getPreviousPageParam`.
62 - `options.cancelRefetch: boolean` same as for `fetchNextPage`.
63- `hasNextPage: boolean`
64 - This will be `true` if there is a next page to be fetched (known via the `getNextPageParam` option).
65- `hasPreviousPage: boolean`
66 - This will be `true` if there is a previous page to be fetched (known via the `getPreviousPageParam` option).