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

injectInfiniteQuery.md

445 lines30.2 KB

1---
2id: injectInfiniteQuery
3title: injectInfiniteQuery
4---
5​
6## Overview
7​
8```ts
9function injectInfiniteQuery<TQueryFnData, TError, TData, TQueryKey, TPageParam>(injectInfiniteQueryFn: () => DefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, options?: InjectInfiniteQueryOptions): DefinedCreateInfiniteQueryResult<TData, TError>;
10function injectInfiniteQuery<TQueryFnData, TError, TData, TQueryKey, TPageParam>(injectInfiniteQueryFn: () => UndefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, options?: InjectInfiniteQueryOptions): CreateInfiniteQueryResult<TData, TError>;
11function injectInfiniteQuery<TQueryFnData, TError, TData, TQueryKey, TPageParam>(injectInfiniteQueryFn: () => CreateInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, options?: InjectInfiniteQueryOptions): CreateInfiniteQueryResult<TData, TError>;
12```
13​
14- [`() => DefinedInitialDataInfiniteOptions` β†’ `DefinedCreateInfiniteQueryResult`](#call-signature-1): The options for `injectInfiniteQuery` are identical to `injectQuery`, with the addition of `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`. Infinite queries can additively "load more" data onto an existing set of data, or "infinite scroll".
15- [`() => UndefinedInitialDataInfiniteOptions` β†’ `CreateInfiniteQueryResult`](#call-signature-2): Injects an infinite query: a declarative dependency on an asynchronous source of data that is tied to a unique key. Infinite queries can additively "load more" data onto an existing set of data, or "infinite scroll".
16- [`() => CreateInfiniteQueryOptions` β†’ `CreateInfiniteQueryResult`](#call-signature-3): This overload accepts the general [CreateInfiniteQueryOptions](../interfaces/CreateInfiniteQueryOptions.md) shape rather than the `initialData`-aware overloads above, so whether `data` is defined can't be inferred from the call site β€” useful when wrapping `injectInfiniteQuery` in your own helper function that forwards caller-provided options.
17​
18See also: [Parameters](#parameters-summary) Β· [Returns](#returns-summary)
19​
20<a id="call-signature-1"></a>
21​
22## Call Signature
23​
24```ts
25function injectInfiniteQuery<TQueryFnData, TError, TData, TQueryKey, TPageParam>(injectInfiniteQueryFn: () => DefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, options?: InjectInfiniteQueryOptions): DefinedCreateInfiniteQueryResult<TData, TError>;
26```
27​
28Defined in: [packages/angular-query-experimental/src/inject-infinite-query.ts:85](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-infinite-query.ts#L85)
29​
30The options for `injectInfiniteQuery` are identical to `injectQuery`, with the addition of
31`initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`. Infinite queries can
32additively "load more" data onto an existing set of data, or "infinite scroll".
33​
34This overload is selected when `initialData` is set on the options returned by `injectInfiniteQueryFn`,
35so the resulting `data` signal is never `undefined` (unless a `select` changes `TData` to include `undefined`).
36​
37### Type Parameters
38​
39#### TQueryFnData
40​
41`TQueryFnData`
42​
43#### TError
44​
45`TError` = `Error`
46​
47#### TData
48​
49`TData` = [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `unknown`\>
50​
51#### TQueryKey
52​
53`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
54​
55#### TPageParam
56​
57`TPageParam` = `unknown`
58​
59### Parameters
60​
61#### injectInfiniteQueryFn
62​
63() => [`DefinedInitialDataInfiniteOptions`](../type-aliases/DefinedInitialDataInfiniteOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
64​
65A function returning the [DefinedInitialDataInfiniteOptions](../type-aliases/DefinedInitialDataInfiniteOptions.md) to use β€”
66everything you can pass to `injectInfiniteQuery`, with `initialData` set. Similar to `computed` from
67Angular, this function runs in the reactive context, so signals read inside it drive the query.
68​
69#### options?
70​
71[`InjectInfiniteQueryOptions`](../interfaces/InjectInfiniteQueryOptions.md)
72​
73Additional configuration.
74​
75### Returns
76​
77[`DefinedCreateInfiniteQueryResult`](../type-aliases/DefinedCreateInfiniteQueryResult.md)\<`TData`, `TError`\>
78​
79The same signals as `injectQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
80`hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data().pages` and
81`data().pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
82`InfiniteData<TQueryFnData>` shape.
83​
84### Remarks
85​
86Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
87refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user
88actions, or add conditions like `hasNextPage() && !isFetching()`.
89​
90### See
91​
92[infiniteQueryOptions](infiniteQueryOptions.md) to share these options between `injectInfiniteQuery` and imperative APIs
93like `queryClient.fetchInfiniteQuery`.
94​
95### Example
96​
97```angular-ts
98@Component({
99 selector: 'projects',
100 template: `
101 <!-- `projectsQuery.data()` is never `undefined`, thanks to `initialData` β€” even if a
102 refetch fails, so the list stays visible alongside the error. -->
103 <ul>
104 @for (page of projectsQuery.data().pages; track $index) {
105 @for (project of page.projects; track project.id) {
106 <li>{{ project.name }}</li>
107 }
108 }
109 </ul>
110 `,
111})
112export class Projects {
113 readonly projectsQuery = injectInfiniteQuery(() => ({
114 queryKey: ['projects'],
115 queryFn: ({ pageParam }) => fetchProjects(pageParam),
116 initialPageParam: 0,
117 getNextPageParam: (lastPage) => lastPage.nextId,
118 initialData: { pages: [], pageParams: [] },
119 }))
121```
122​
123<a id="call-signature-2"></a>
124​
125## Call Signature
126​
127```ts
128function injectInfiniteQuery<TQueryFnData, TError, TData, TQueryKey, TPageParam>(injectInfiniteQueryFn: () => UndefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, options?: InjectInfiniteQueryOptions): CreateInfiniteQueryResult<TData, TError>;
129```
130​
131Defined in: [packages/angular-query-experimental/src/inject-infinite-query.ts:237](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-infinite-query.ts#L237)
132​
133Injects an infinite query: a declarative dependency on an asynchronous source of data that is tied to a
134unique key. Infinite queries can additively "load more" data onto an existing set of data, or
135"infinite scroll".
136​
137### Type Parameters
138​
139#### TQueryFnData
140​
141`TQueryFnData`
142​
143#### TError
144​
145`TError` = `Error`
146​
147#### TData
148​
149`TData` = [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `unknown`\>
150​
151#### TQueryKey
152​
153`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
154​
155#### TPageParam
156​
157`TPageParam` = `unknown`
158​
159### Parameters
160​
161#### injectInfiniteQueryFn
162​
163() => [`UndefinedInitialDataInfiniteOptions`](../type-aliases/UndefinedInitialDataInfiniteOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
164​
165A function returning the [UndefinedInitialDataInfiniteOptions](../type-aliases/UndefinedInitialDataInfiniteOptions.md) to use
166β€” everything you can pass to `injectInfiniteQuery`. Similar to `computed` from Angular, this function runs
167in the reactive context, so signals read inside it drive the query.
168​
169#### options?
170​
171[`InjectInfiniteQueryOptions`](../interfaces/InjectInfiniteQueryOptions.md)
172​
173Additional configuration.
174​
175### Returns
176​
177[`CreateInfiniteQueryResult`](../type-aliases/CreateInfiniteQueryResult.md)\<`TData`, `TError`\>
178​
179The same signals as `injectQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
180`hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data().pages` and
181`data().pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
182`InfiniteData<TQueryFnData>` shape.
183​
184### Remarks
185​
186Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
187refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user
188actions, or add conditions like `hasNextPage() && !isFetching()`. This is the only overload that accepts
189`queryFn: skipToken`, shown below.
190​
191### See
192​
193[infiniteQueryOptions](infiniteQueryOptions.md) to share these options between `injectInfiniteQuery` and imperative APIs
194like `queryClient.fetchInfiniteQuery`.
195​
196### Examples
197​
198Fetching the next page from a button click:
199```angular-ts
200@Component({
201 selector: 'projects-list',
202 template: `
203 <ul>
204 @for (page of projectsQuery.data()?.pages; track $index) {
205 @for (project of page.projects; track project.id) {
206 <li>{{ project.name }}</li>
207 }
208 }
209 </ul>
210 <button
211 [disabled]="!projectsQuery.hasNextPage() || projectsQuery.isFetching()"
212 (click)="projectsQuery.fetchNextPage()"
213 >
214 Load More
215 </button>
216 `,
217})
218export class ProjectsList {
219 readonly projectsQuery = injectInfiniteQuery(() => ({
220 queryKey: ['projects'],
221 queryFn: ({ pageParam }) => fetchProjects(pageParam),
222 initialPageParam: 0,
223 getNextPageParam: (lastPage) => lastPage.nextId,
224 }))
226```
227​
228Fetching the next page automatically as the user scrolls, using an `IntersectionObserver` on a sentinel
229element after the list:
230```angular-ts
231@Component({
232 selector: 'projects-list',
233 template: `
234 <ul>
235 @for (page of projectsQuery.data()?.pages; track $index) {
236 @for (project of page.projects; track project.id) {
237 <li>{{ project.name }}</li>
238 }
239 }
240 </ul>
241 <div #sentinel>{{ projectsQuery.isFetchingNextPage() ? 'Loading more...' : '' }}</div>
242 `,
243})
244export class ProjectsList {
245 readonly sentinel = viewChild<ElementRef<HTMLElement>>('sentinel')
246​
247 readonly projectsQuery = injectInfiniteQuery(() => ({
248 queryKey: ['projects'],
249 queryFn: ({ pageParam }) => fetchProjects(pageParam),
250 initialPageParam: 0,
251 getNextPageParam: (lastPage) => lastPage.nextId,
252 }))
253​
254 constructor() {
255 effect((onCleanup) => {
256 const sentinel = this.sentinel()?.nativeElement
257 if (
258 sentinel == null ||
259 !this.projectsQuery.hasNextPage() ||
260 this.projectsQuery.isFetching()
261 ) {
262 return
263 }
264​
265 const observer = new IntersectionObserver(([entry]) => {
266 if (entry?.isIntersecting) this.projectsQuery.fetchNextPage()
267 })
268 observer.observe(sentinel)
269​
270 onCleanup(() => observer.disconnect())
271 })
272 }
274```
275​
276A query that's disabled, type safe, until `postId` is set β€” pass `skipToken` as `queryFn` instead of
277setting `enabled: false`:
278```angular-ts
279@Component({
280 selector: 'comments',
281 template: `
282 @if (postId() == null) {
283 Select a post
284 } @else if (commentsQuery.isPending()) {
285 Loading...
286 } @else if (commentsQuery.isError()) {
287 <span>Error: {{ commentsQuery.error()?.message }}</span>
288 } @else {
289 <ul>
290 @for (page of commentsQuery.data().pages; track $index) {
291 @for (comment of page.comments; track comment.id) {
292 <li>{{ comment.text }}</li>
293 }
294 }
295 </ul>
296 }
297 `,
298})
299export class Comments {
300 readonly postId = signal<string | undefined>(undefined)
301​
302 readonly commentsQuery = injectInfiniteQuery(() => ({
303 queryKey: ['post', this.postId(), 'comments'],
304 queryFn:
305 this.postId() != null
306 ? ({ pageParam }) => fetchComments(this.postId()!, pageParam)
307 : skipToken,
308 initialPageParam: 0,
309 getNextPageParam: (lastPage) => lastPage.nextId,
310 }))
312```
313​
314<a id="call-signature-3"></a>
315​
316## Call Signature
317​
318```ts
319function injectInfiniteQuery<TQueryFnData, TError, TData, TQueryKey, TPageParam>(injectInfiniteQueryFn: () => CreateInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, options?: InjectInfiniteQueryOptions): CreateInfiniteQueryResult<TData, TError>;
320```
321​
322Defined in: [packages/angular-query-experimental/src/inject-infinite-query.ts:264](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-infinite-query.ts#L264)
323​
324This overload accepts the general [CreateInfiniteQueryOptions](../interfaces/CreateInfiniteQueryOptions.md) shape rather than the
325`initialData`-aware overloads above, so whether `data` is defined can't be inferred from the call site β€”
326useful when wrapping `injectInfiniteQuery` in your own helper function that forwards caller-provided
327options.
328​
329### Type Parameters
330​
331#### TQueryFnData
332​
333`TQueryFnData`
334​
335#### TError
336​
337`TError` = `Error`
338​
339#### TData
340​
341`TData` = [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `unknown`\>
342​
343#### TQueryKey
344​
345`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
346​
347#### TPageParam
348​
349`TPageParam` = `unknown`
350​
351### Parameters
352​
353#### injectInfiniteQueryFn
354​
355() => [`CreateInfiniteQueryOptions`](../interfaces/CreateInfiniteQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
356​
357A function that returns infinite query options. Similar to `computed` from
358Angular, this function runs in the reactive context, so signals read inside it drive the query.
359​
360#### options?
361​
362[`InjectInfiniteQueryOptions`](../interfaces/InjectInfiniteQueryOptions.md)
363​
364Additional configuration.
365​
366### Returns
367​
368[`CreateInfiniteQueryResult`](../type-aliases/CreateInfiniteQueryResult.md)\<`TData`, `TError`\>
369​
370The infinite query result.
371​
372<a id="parameters-summary"></a>
373​
374## Parameters
375​
376### injectInfiniteQueryFn
377​
378() => [`CreateInfiniteQueryOptions`](../interfaces/CreateInfiniteQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
379​
380A function that returns infinite query options. Similar to `computed` from
381Angular, this function runs in the reactive context, so signals read inside it drive the query.
382​
383<a id="injectInfiniteQueryFn-properties"></a>
384​
385#### `injectInfiniteQueryFn` properties
386​
387| Property | Type | Default value | Description |
388| ------ | ------ | ------ | ------ |
389| <a id="injectInfiniteQueryFn-property-enabled"></a> `enabled?` | \| `false` \| `true` \| ((`query`: [`Query`](../classes/Query.md)\<`TQueryFnData`, `TError`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\>, `TQueryKey`\>) => `boolean`) | `true` | Set this to `false` or a function that returns `false` to disable automatic refetching when the query mounts or changes query keys. To refetch the query, use the `refetch` method returned from the `useQuery` instance. Accepts a boolean or function that returns a boolean. |
390| <a id="injectInfiniteQueryFn-property-gctime"></a> `gcTime?` | `number` | `undefined` | The time in milliseconds that unused/inactive cache data remains in memory. When a query's cache becomes unused or inactive, that cache data will be garbage collected after this duration. When different garbage collection times are specified, the longest one will be used. Setting it to `Infinity` will disable garbage collection. Defaults to `5 * 60 * 1000` (5 minutes), or `Infinity` during SSR. Note: the maximum allowed time is about 24 days, imposed by `setTimeout`'s 32-bit signed integer delay β€” see `timeoutManager.setTimeoutProvider` for a workaround. |
391| <a id="injectInfiniteQueryFn-property-getnextpageparam"></a> `getNextPageParam` | (`lastPage`: `TQueryFnData`, `allPages`: `TQueryFnData`[], `lastPageParam`: `TPageParam`, `allPageParams`: `TPageParam`[]) => `TPageParam` \| `null` \| `undefined` | `undefined` | This function can be set to automatically get the next cursor for infinite queries. The result will also be used to determine the value of `hasNextPage`. |
392| <a id="injectInfiniteQueryFn-property-getpreviouspageparam"></a> `getPreviousPageParam?` | (`firstPage`: `TQueryFnData`, `allPages`: `TQueryFnData`[], `firstPageParam`: `TPageParam`, `allPageParams`: `TPageParam`[]) => `TPageParam` \| `null` \| `undefined` | `undefined` | This function can be set to automatically get the previous cursor for infinite queries. The result will also be used to determine the value of `hasPreviousPage`. |
393| <a id="injectInfiniteQueryFn-property-initialdata"></a> `initialData?` | \| [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\> \| (() => \| [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\> \| `undefined`) | `undefined` | If set, this value will be used as the initial data for the query cache (as long as the query hasn't been created or cached yet). If set to a function, the function will be called **once** during the shared/root query initialization, and be expected to synchronously return the initial data. Initial data is considered stale by default unless a `staleTime` has been set. `initialData` **is persisted** to the cache. |
394| <a id="injectInfiniteQueryFn-property-initialdataupdatedat"></a> `initialDataUpdatedAt?` | `number` \| (() => `number` \| `undefined`) | `undefined` | If set, this value will be used as the time (in milliseconds) of when the `initialData` itself was last updated. |
395| <a id="injectInfiniteQueryFn-property-initialpageparam"></a> `initialPageParam` | `TPageParam` | `undefined` | The page param to start from when an infinite query has no pages yet. It is passed to `queryFn` as `pageParam` for the first page; every page after that gets the value returned by `getNextPageParam` or `getPreviousPageParam`. It only applies while the query has no pages: once a first page exists, refetching starts from that page's own param instead. |
396| <a id="injectInfiniteQueryFn-property-maxpages"></a> `maxPages?` | `number` | `undefined` | Maximum number of pages to store in the data of an infinite query. |
397| <a id="injectInfiniteQueryFn-property-meta"></a> `meta?` | `Record`\<`string`, `unknown`\> | `undefined` | Additional payload to be stored on each query. Use this property to pass information that can be used in other places. |
398| <a id="injectInfiniteQueryFn-property-networkmode"></a> `networkMode?` | `"online"` \| `"always"` \| `"offlineFirst"` | `'online'` | Controls whether a query is allowed to run based on the current network connectivity. **See** [Network Mode](https://tanstack.com/query/latest/docs/framework/react/guides/network-mode) for more information. |
399| <a id="injectInfiniteQueryFn-property-notifyonchangeprops"></a> `notifyOnChangeProps?` | \| ( \| `"error"` \| `"data"` \| `"isError"` \| `"isPending"` \| `"isLoading"` \| `"isLoadingError"` \| `"isRefetchError"` \| `"isSuccess"` \| `"isPlaceholderData"` \| `"status"` \| `"dataUpdatedAt"` \| `"errorUpdatedAt"` \| `"failureCount"` \| `"failureReason"` \| `"errorUpdateCount"` \| `"isFetched"` \| `"isFetchedAfterMount"` \| `"isFetching"` \| `"isInitialLoading"` \| `"isPaused"` \| `"isRefetching"` \| `"isStale"` \| `"isEnabled"` \| `"refetch"` \| `"fetchStatus"` \| `"fetchNextPage"` \| `"fetchPreviousPage"` \| `"hasNextPage"` \| `"hasPreviousPage"` \| `"isFetchNextPageError"` \| `"isFetchingNextPage"` \| `"isFetchPreviousPageError"` \| `"isFetchingPreviousPage"`)[] \| `"all"` \| (() => \| `"all"` \| ( \| `"error"` \| `"data"` \| `"isError"` \| `"isPending"` \| `"isLoading"` \| `"isLoadingError"` \| `"isRefetchError"` \| `"isSuccess"` \| `"isPlaceholderData"` \| `"status"` \| `"dataUpdatedAt"` \| `"errorUpdatedAt"` \| `"failureCount"` \| `"failureReason"` \| `"errorUpdateCount"` \| `"isFetched"` \| `"isFetchedAfterMount"` \| `"isFetching"` \| `"isInitialLoading"` \| `"isPaused"` \| `"isRefetching"` \| `"isStale"` \| `"isEnabled"` \| `"refetch"` \| `"fetchStatus"` \| `"fetchNextPage"` \| `"fetchPreviousPage"` \| `"hasNextPage"` \| `"hasPreviousPage"` \| `"isFetchNextPageError"` \| `"isFetchingNextPage"` \| `"isFetchPreviousPageError"` \| `"isFetchingPreviousPage"`)[] \| `undefined`) | `undefined` | If set, the component will only re-render if any of the listed properties change. When set to `['data', 'error']`, the component will only re-render when the `data` or `error` properties change. When set to `'all'`, the component will re-render whenever a query is updated. When set to a function, the function will be executed to compute the list of properties. Defaults to `undefined`, in which case property access is tracked automatically, and the component only re-renders when one of the tracked properties changes. |
400| <a id="injectInfiniteQueryFn-property-persister"></a> `persister?` | (`queryFn`: (`context`: [`QueryFunctionContext`](../type-aliases/QueryFunctionContext.md)\<`NoInfer`\<`TQueryKey`\>, `TPageParam`\>) => `TQueryFnData` \| `Promise`\<`TQueryFnData`\>, `context`: `object`, `query`: [`Query`](../classes/Query.md)) => `TQueryFnData` \| `Promise`\<`TQueryFnData`\> | `undefined` | This option can be used to persist the result of a query to an external storage, bypassing the need to actually call the `queryFn`. Useful for persisting a query's data across e.g. server/client boundaries. |
401| <a id="injectInfiniteQueryFn-property-placeholderdata"></a> `placeholderData?` | \| [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\> \| ((`previousData`: \| [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\> \| `undefined`, `previousQuery`: \| [`Query`](../classes/Query.md)\<[`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\>, `TError`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\>, `TQueryKey`\> \| `undefined`) => \| [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\> \| `undefined`) | `undefined` | If set, this value will be used as the placeholder data for this particular query observer while the query is still in the `loading` data and no initialData has been provided. |
402| <a id="injectInfiniteQueryFn-property-queryfn"></a> `queryFn?` | \| *typeof* [`skipToken`](../variables/skipToken.md) \| ((`context`: [`QueryFunctionContext`](../type-aliases/QueryFunctionContext.md)\<`TQueryKey`, `TPageParam`\>) => `TQueryFnData` \| `Promise`\<`TQueryFnData`\>) | `undefined` | The function that the query will use to request data. Required, unless a default query function has been set via `queryClient.setQueryDefaults` or `queryClient.setDefaultOptions`. Receives a [QueryFunctionContext](../type-aliases/QueryFunctionContext.md). Must return a promise that will either resolve data or throw an error. The data cannot be `undefined`. |
403| <a id="injectInfiniteQueryFn-property-queryhash"></a> `queryHash?` | `string` | `undefined` | The hashed form of `queryKey`, computed with `queryKeyHashFn` (or the default hashing function otherwise). Used as the actual cache key internally. |
404| <a id="injectInfiniteQueryFn-property-querykey"></a> `queryKey` | `TQueryKey` & `object` | `undefined` | The query key to use for this query. The query key will be hashed into a stable hash. See [Query Keys](https://tanstack.com/query/latest/docs/framework/react/guides/query-keys) for more information. The query will automatically update when this key changes (as long as `enabled` is not set to `false`). |
405| <a id="injectInfiniteQueryFn-property-querykeyhashfn"></a> `queryKeyHashFn?` | (`queryKey`: `TQueryKey`) => `string` | `undefined` | If specified, this function is used to hash the `queryKey` to a string. |
406| <a id="injectInfiniteQueryFn-property-refetchinterval"></a> `refetchInterval?` | \| `number` \| `false` \| ((`query`: [`Query`](../classes/Query.md)\<`TQueryFnData`, `TError`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\>, `TQueryKey`\>) => `number` \| `false` \| `undefined`) | `false` | If set to a number, the query will continuously refetch at this frequency in milliseconds. If set to a function, the function will be executed with the latest data and query to compute a frequency |
407| <a id="injectInfiniteQueryFn-property-refetchintervalinbackground"></a> `refetchIntervalInBackground?` | `boolean` | `false` | If set to `true`, the query will continue to refetch while their tab/window is in the background. |
408| <a id="injectInfiniteQueryFn-property-refetchonmount"></a> `refetchOnMount?` | \| `boolean` \| `"always"` \| ((`query`: [`Query`](../classes/Query.md)\<`TQueryFnData`, `TError`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\>, `TQueryKey`\>) => `boolean` \| `"always"`) | `true` | If set to `true`, the query will refetch on mount if the data is stale. If set to `false`, will disable additional instances of a query to trigger background refetch. If set to `'always'`, the query will always refetch on mount (except when `staleTime: 'static'` is used). If set to a function, the function will be executed with the latest data and query to compute the value |
409| <a id="injectInfiniteQueryFn-property-refetchonreconnect"></a> `refetchOnReconnect?` | \| `boolean` \| `"always"` \| ((`query`: [`Query`](../classes/Query.md)\<`TQueryFnData`, `TError`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\>, `TQueryKey`\>) => `boolean` \| `"always"`) | `undefined` | If set to `true`, the query will refetch on reconnect if the data is stale. If set to `false`, the query will not refetch on reconnect. If set to `'always'`, the query will always refetch on reconnect (except when `staleTime: 'static'` is used). If set to a function, the function will be executed with the latest data and query to compute the value. Defaults to `true` unless `networkMode` is `'always'`. |
410| <a id="injectInfiniteQueryFn-property-refetchonwindowfocus"></a> `refetchOnWindowFocus?` | \| `boolean` \| `"always"` \| ((`query`: [`Query`](../classes/Query.md)\<`TQueryFnData`, `TError`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\>, `TQueryKey`\>) => `boolean` \| `"always"`) | `true` | If set to `true`, the query will refetch on window focus if the data is stale. If set to `false`, the query will not refetch on window focus. If set to `'always'`, the query will always refetch on window focus (except when `staleTime: 'static'` is used). If set to a function, the function will be executed with the latest data and query to compute the value. |
411| <a id="injectInfiniteQueryFn-property-retry"></a> `retry?` | \| `number` \| `false` \| `true` \| ((`failureCount`: `number`, `error`: `TError`) => `boolean`) | `undefined` | If `false`, failed queries will not retry by default. If `true`, failed queries will retry infinitely. If set to an integer number, e.g. 3, failed queries will retry until the failed query count meets that number. If set to a function `(failureCount, error) => boolean` failed queries will retry until the function returns false. Defaults to `3` on the client and `0` on the server. |
412| <a id="injectInfiniteQueryFn-property-retrydelay"></a> `retryDelay?` | `number` \| ((`failureCount`: `number`, `error`: `TError`) => `number`) | `undefined` | This function receives a `retryAttempt` integer and the actual Error and returns the delay to apply before the next attempt in milliseconds. A function like `attempt => Math.min(attempt > 1 ? 2 ** attempt * 1000 : 1000, 30 * 1000)` applies exponential backoff. A function like `attempt => attempt * 1000` applies linear backoff. Defaults to a function that applies exponential backoff, capped at 30 seconds. |
413| <a id="injectInfiniteQueryFn-property-retryonmount"></a> `retryOnMount?` | \| `false` \| `true` \| ((`query`: [`Query`](../classes/Query.md)\<`TQueryFnData`, `TError`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\>, `TQueryKey`\>) => `boolean`) | `true` | If set to `false`, the query will not be retried on mount if it contains an error. If set to a function, the function will be executed with the query to compute the value. |
414| <a id="injectInfiniteQueryFn-property-select"></a> `select?` | (`data`: [`InfiniteData`](../interfaces/InfiniteData.md)) => `TData` | `undefined` | This option can be used to transform or select a part of the data returned by the query function. It affects the returned `data` value, but does not affect what gets stored in the query cache. The `select` function will only run if `data` changed, or if the reference to the `select` function itself changes. To optimize, memoize the function so its reference stays stable across calls. |
415| <a id="injectInfiniteQueryFn-property-staletime"></a> `staleTime?` | \| `number` \| `"static"` \| ((`query`: [`Query`](../classes/Query.md)\<`TQueryFnData`, `TError`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\>, `TQueryKey`\>) => `number` \| `"static"`) | `0` | The time in milliseconds after data is considered stale. If set to `Infinity`, the data will never be considered stale. If set to `'static'`, the data will never be considered stale. If set to a function, the function will be executed with the query to compute a `staleTime`. |
416| <a id="injectInfiniteQueryFn-property-structuralsharing"></a> `structuralSharing?` | `boolean` \| ((`oldData`: `unknown`, `newData`: `unknown`) => `unknown`) | `true` | Set this to `false` to disable structural sharing between query results. Set this to a function which accepts the old and new data and returns resolved data of the same type to implement custom structural sharing logic. |
417| <a id="injectInfiniteQueryFn-property-throwonerror"></a> `throwOnError?` | \| `false` \| `true` \| ((`error`: `TError`, `query`: [`Query`](../classes/Query.md)\<`TQueryFnData`, `TError`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\>, `TQueryKey`\>) => `boolean`) | `false` | Whether errors should be thrown instead of setting the `error` property. If set to `true` or `suspense` is `true`, all errors will be thrown to the error boundary. If set to `false` and `suspense` is `false`, errors are returned as state. If set to a function, it will be passed the error and the query, and it should return a boolean indicating whether to show the error in an error boundary (`true`) or return the error as state (`false`). |
418​
419### options?
420​
421[`InjectInfiniteQueryOptions`](../interfaces/InjectInfiniteQueryOptions.md)
422​
423Additional configuration.
424​
425<a id="options-properties"></a>
426​
427#### `options` properties
428​
429| Property | Type | Description |
430| ------ | ------ | ------ |
431| <a id="options-property-injector"></a> `injector?` | `Injector` | The `Injector` in which to create the infinite query. If this is not provided, the current injection context will be used instead (via `inject`). |
432​
433<a id="returns-summary"></a>
434​
435## Returns
436​
437[`CreateInfiniteQueryResult`](../type-aliases/CreateInfiniteQueryResult.md)\<`TData`, `TError`\>
438​
439The infinite query result.
440​
441<a id="result-properties"></a>
442​
443### Result properties
444​
445Built from [`BaseQueryNarrowing`](../interfaces/BaseQueryNarrowing.md#properties), [`InfiniteQueryObserverBaseResult`](../interfaces/InfiniteQueryObserverBaseResult.md#properties). See the type above for what it changes.