Open History to see the last commit
1---
2id: createInfiniteQuery
3title: createInfiniteQuery
4---
5β
6```ts
7const createInfiniteQuery: {
8<TQueryFnData, TError, TData, TQueryKey, TPageParam> (options: DefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient?: Accessor<QueryClient>): DefinedUseInfiniteQueryResult<TData, TError>;
9<TQueryFnData, TError, TData, TQueryKey, TPageParam> (options: UndefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient?: Accessor<QueryClient>): UseInfiniteQueryResult<TData, TError>;
10} = useInfiniteQuery;
11```
12β
13Defined in: [packages/solid-query/src/index.ts:72](https://github.com/TanStack/query/blob/main/packages/solid-query/src/index.ts#L72)
14β
15## Call Signature
16β
17```ts
18<TQueryFnData, TError, TData, TQueryKey, TPageParam>(options: DefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient?: Accessor<QueryClient>): DefinedUseInfiniteQueryResult<TData, TError>;
19```
20β
21The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of
22`initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
23β
24This overload is selected when `initialData` is set.
25β
26### Type Parameters
27β
28#### TQueryFnData
29β
30`TQueryFnData`
31β
32#### TError
33β
34`TError` = `Error`
35β
36#### TData
37β
38`TData` = [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `unknown`\>
39β
40#### TQueryKey
41β
42`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
43β
44#### TPageParam
45β
46`TPageParam` = `unknown`
47β
48### Parameters
49β
50#### options
51β
52[`DefinedInitialDataInfiniteOptions`](../type-aliases/DefinedInitialDataInfiniteOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
53β
54An accessor returning the [DefinedInitialDataInfiniteOptions](../type-aliases/DefinedInitialDataInfiniteOptions.md) to use β everything you
55can pass to `useInfiniteQuery`, with `initialData` set.
56β
57#### queryClient?
58β
59`Accessor`\<[`QueryClient`](../classes/QueryClient.md)\>
60β
61An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
62will be used.
63β
64### Returns
65β
66[`DefinedUseInfiniteQueryResult`](../type-aliases/DefinedUseInfiniteQueryResult.md)\<`TData`, `TError`\>
67β
68The same properties as `useQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
69`hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data.pages` and
70`data.pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
71`InfiniteData<TQueryFnData>` shape.
72β
73### Remarks
74β
75Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
76refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user
77actions, or add conditions like `hasNextPage && !isFetching`.
78β
79### See
80β
81[infiniteQueryOptions](../functions/infiniteQueryOptions.md) to share these options between `useInfiniteQuery` and imperative APIs like `queryClient.infiniteQuery`.
82β
83### Example
84β
85```tsx
86import { For } from 'solid-js'
87import { useInfiniteQuery } from '@tanstack/solid-query'
88β
89function Projects() {
90 // `projectsQuery.data` is never `undefined`, thanks to `initialData` β even if a refetch fails, so the
91 // list stays visible alongside the error.
92 const projectsQuery = useInfiniteQuery(() => ({
93 queryKey: ['projects'],
94 queryFn: ({ pageParam }) => fetchProjects(pageParam),
95 initialPageParam: 0,
96 getNextPageParam: (lastPage) => lastPage.nextId,
97 initialData: { pages: [], pageParams: [] },
98 }))
99β
100 return (
101 <div>
102 {projectsQuery.isError ? <span>Error: {projectsQuery.error.message}</span> : null}
103 <ul>
104 <For each={projectsQuery.data.pages}>
105 {(page) => <For each={page.projects}>{(p) => <li>{p.name}</li>}</For>}
106 </For>
107 </ul>
108 </div>
109 )
110}
111```
112β
113## Call Signature
114β
115```ts
116<TQueryFnData, TError, TData, TQueryKey, TPageParam>(options: UndefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient?: Accessor<QueryClient>): UseInfiniteQueryResult<TData, TError>;
117```
118β
119The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of
120`initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
121β
122### Type Parameters
123β
124#### TQueryFnData
125β
126`TQueryFnData`
127β
128#### TError
129β
130`TError` = `Error`
131β
132#### TData
133β
134`TData` = [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `unknown`\>
135β
136#### TQueryKey
137β
138`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
139β
140#### TPageParam
141β
142`TPageParam` = `unknown`
143β
144### Parameters
145β
146#### options
147β
148[`UndefinedInitialDataInfiniteOptions`](../type-aliases/UndefinedInitialDataInfiniteOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
149β
150An accessor returning the [UndefinedInitialDataInfiniteOptions](../type-aliases/UndefinedInitialDataInfiniteOptions.md) to use β everything
151you can pass to `useInfiniteQuery`.
152β
153#### queryClient?
154β
155`Accessor`\<[`QueryClient`](../classes/QueryClient.md)\>
156β
157An accessor for a custom `QueryClient`. Otherwise, the one from the nearest context
158will be used.
159β
160### Returns
161β
162[`UseInfiniteQueryResult`](../type-aliases/UseInfiniteQueryResult.md)\<`TData`, `TError`\>
163β
164The same properties as `useQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
165`hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data.pages` and
166`data.pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
167`InfiniteData<TQueryFnData>` shape.
168β
169### Remarks
170β
171Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
172refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user
173actions, or add conditions like `hasNextPage && !isFetching`.
174β
175### See
176β
177[infiniteQueryOptions](../functions/infiniteQueryOptions.md) to share these options between `useInfiniteQuery` and imperative APIs like `queryClient.infiniteQuery`.
178β
179### Examples
180β
181Fetching the next page from a "Load More" button click:
182```tsx
183import { For, Match, Switch } from 'solid-js'
184import { useInfiniteQuery } from '@tanstack/solid-query'
185β
186function Projects() {
187 const projectsQuery = useInfiniteQuery(() => ({
188 queryKey: ['projects'],
189 queryFn: ({ pageParam }) => fetchProjects(pageParam),
190 initialPageParam: 0,
191 getNextPageParam: (lastPage) => lastPage.nextId,
192 }))
193β
194 return (
195 <Switch>
196 <Match when={projectsQuery.isPending}>Loading...</Match>
197 <Match when={projectsQuery.isError}>Error: {projectsQuery.error.message}</Match>
198 <Match when={projectsQuery.isSuccess}>
199 <ul>
200 <For each={projectsQuery.data.pages}>
201 {(page) => <For each={page.projects}>{(p) => <li>{p.name}</li>}</For>}
202 </For>
203 </ul>
204 <button
205 onClick={() => projectsQuery.fetchNextPage()}
206 disabled={!projectsQuery.hasNextPage || projectsQuery.isFetching}
207 >
208 {projectsQuery.isFetchingNextPage
209 ? 'Loading more...'
210 : projectsQuery.hasNextPage
211 ? 'Load More'
212 : 'Nothing more to load'}
213 </button>
214 </Match>
215 </Switch>
216 )
217}
218```
219β
220Fetching the next page automatically as the user scrolls, using an `IntersectionObserver` on a
221sentinel element after the list:
222```tsx
223import { For, Match, Switch, createEffect, onCleanup } from 'solid-js'
224import { useInfiniteQuery } from '@tanstack/solid-query'
225β
226function Projects() {
227 const projectsQuery = useInfiniteQuery(() => ({
228 queryKey: ['projects'],
229 queryFn: ({ pageParam }) => fetchProjects(pageParam),
230 initialPageParam: 0,
231 getNextPageParam: (lastPage) => lastPage.nextId,
232 }))
233β
234 let sentinelRef: HTMLDivElement | undefined
235β
236 createEffect(() => {
237 if (sentinelRef == null || !projectsQuery.hasNextPage || projectsQuery.isFetching) return
238β
239 const observer = new IntersectionObserver(([entry]) => {
240 if (entry?.isIntersecting) projectsQuery.fetchNextPage()
241 })
242 observer.observe(sentinelRef)
243β
244 onCleanup(() => observer.disconnect())
245 })
246β
247 return (
248 <Switch>
249 <Match when={projectsQuery.isPending}>Loading...</Match>
250 <Match when={projectsQuery.isError}>Error: {projectsQuery.error.message}</Match>
251 <Match when={projectsQuery.isSuccess}>
252 <ul>
253 <For each={projectsQuery.data.pages}>
254 {(page) => <For each={page.projects}>{(p) => <li>{p.name}</li>}</For>}
255 </For>
256 </ul>
257 <div ref={sentinelRef}>{projectsQuery.isFetchingNextPage ? 'Loading more...' : null}</div>
258 </Match>
259 </Switch>
260 )
261}
262```