|
| 1 | +--- |
| 2 | +title: useInfiniteQuery |
| 3 | +--- |
| 4 | +# {{ $frontmatter.title }} |
| 5 | + |
| 6 | +The `useInfiniteQuery` method allows you to use the original [useInfiniteQuery](https://tanstack.com/query/latest/docs/framework/react/reference/useInfiniteQuery) from React Query with OpenAPI typed endpoints. |
| 7 | + |
| 8 | +- The result is the same as the original function. |
| 9 | +- The `queryKey` is `[method, path, params]`. |
| 10 | +- `data` and `error` are fully typed based on your OpenAPI schema. |
| 11 | +- Pagination is automatically handled using the page parameter and a pre-configured `getNextPageParam` function. |
| 12 | + |
| 13 | +::: tip |
| 14 | +You can find more information about `useInfiniteQuery` on the [@tanstack/react-query documentation](https://tanstack.com/query/latest/docs/framework/react/reference/useInfiniteQuery). |
| 15 | +::: |
| 16 | + |
| 17 | +## Example |
| 18 | + |
| 19 | +::: code-group |
| 20 | + |
| 21 | +```tsx [src/app.tsx] |
| 22 | +import { $api } from "./api"; |
| 23 | + |
| 24 | +export const App = () => { |
| 25 | + const { |
| 26 | + data, |
| 27 | + error, |
| 28 | + fetchNextPage, |
| 29 | + hasNextPage, |
| 30 | + isFetching, |
| 31 | + isFetchingNextPage, |
| 32 | + isLoading, |
| 33 | + isError, |
| 34 | + } = $api.useInfiniteQuery("get", "/users", { |
| 35 | + params: { |
| 36 | + query: { limit: 10 }, |
| 37 | + }, |
| 38 | + }); |
| 39 | + |
| 40 | + if (isLoading) return <p>Loading...</p>; |
| 41 | + if (isError) return <p>Error: {(error as Error).message}</p>; |
| 42 | + |
| 43 | + return ( |
| 44 | + <div> |
| 45 | + {data.pages.map((group, i) => ( |
| 46 | + <React.Fragment key={i}> |
| 47 | + {group.users.map(user => ( |
| 48 | + <p key={user.id}>{user.name}</p> |
| 49 | + ))} |
| 50 | + </React.Fragment> |
| 51 | + ))} |
| 52 | + <div> |
| 53 | + <button |
| 54 | + onClick={() => fetchNextPage()} |
| 55 | + disabled={!hasNextPage || isFetchingNextPage} |
| 56 | + > |
| 57 | + {isFetchingNextPage |
| 58 | + ? 'Loading more...' |
| 59 | + : hasNextPage |
| 60 | + ? 'Load More' |
| 61 | + : 'Nothing more to load'} |
| 62 | + </button> |
| 63 | + </div> |
| 64 | + <div>{isFetching && !isFetchingNextPage ? 'Fetching...' : null}</div> |
| 65 | + </div> |
| 66 | + ); |
| 67 | +}; |
| 68 | +``` |
| 69 | + |
| 70 | +```ts [src/api.ts] |
| 71 | +import createFetchClient from "openapi-fetch"; |
| 72 | +import createClient from "openapi-react-query"; |
| 73 | +import type { paths } from "./my-openapi-3-schema"; // generated by openapi-typescript |
| 74 | + |
| 75 | +const fetchClient = createFetchClient<paths>({ |
| 76 | + baseUrl: "https://myapi.dev/v1/", |
| 77 | +}); |
| 78 | +export const $api = createClient(fetchClient); |
| 79 | +``` |
| 80 | + |
| 81 | +::: |
| 82 | + |
| 83 | +## Api |
| 84 | + |
| 85 | +```tsx |
| 86 | +const query = $api.useInfiniteQuery(method, path, options, queryOptions, queryClient); |
| 87 | +``` |
| 88 | + |
| 89 | +**Arguments** |
| 90 | + |
| 91 | +- `method` **(required)** |
| 92 | + - The HTTP method to use for the request. |
| 93 | + - The method is used as key. See [Query Keys](https://tanstack.com/query/latest/docs/framework/react/guides/query-keys) for more information. |
| 94 | +- `path` **(required)** |
| 95 | + - The pathname to use for the request. |
| 96 | + - Must be an available path for the given method in your schema. |
| 97 | + - The pathname is used as key. See [Query Keys](https://tanstack.com/query/latest/docs/framework/react/guides/query-keys) for more information. |
| 98 | +- `options` |
| 99 | + - The fetch options to use for the request. |
| 100 | + - Only required if the OpenAPI schema requires parameters. |
| 101 | + - The options `params` are used as key. See [Query Keys](https://tanstack.com/query/latest/docs/framework/react/guides/query-keys) for more information. |
| 102 | +- `queryOptions` |
| 103 | + - The original `useInfiniteQuery` options. |
| 104 | + - [See more information](https://tanstack.com/query/latest/docs/framework/react/reference/useInfiniteQuery) |
| 105 | +- `queryClient` |
| 106 | + - The original `queryClient` option. |
| 107 | + - [See more information](https://tanstack.com/query/latest/docs/framework/react/reference/useInfiniteQuery) |
0 commit comments