Open History to see the last commit
1---
2id: injectInfiniteQuery
3title: injectInfiniteQuery
4---
5β
6## Call Signature
7β
8```ts
9function injectInfiniteQuery<TQueryFnData, TError, TData, TQueryKey, TPageParam>(injectInfiniteQueryFn: () => DefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, options?: InjectInfiniteQueryOptions): DefinedCreateInfiniteQueryResult<TData, TError>;
10```
11β
12Defined in: [packages/angular-query-experimental/src/inject-infinite-query.ts:83](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-infinite-query.ts#L83)
13β
14The options for `injectInfiniteQuery` are identical to `injectQuery`, with the addition of
15`initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`. Infinite queries can
16additively "load more" data onto an existing set of data, or "infinite scroll".
17β
18This overload is selected when `initialData` is set on the options returned by `injectInfiniteQueryFn`,
19so the resulting `data` signal is never `undefined` (unless a `select` changes `TData` to include `undefined`).
20β
21### Type Parameters
22β
23#### TQueryFnData
24β
25`TQueryFnData`
26β
27#### TError
28β
29`TError` = `Error`
30β
31#### TData
32β
33`TData` = [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `unknown`\>
34β
35#### TQueryKey
36β
37`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
38β
39#### TPageParam
40β
41`TPageParam` = `unknown`
42β
43### Parameters
44β
45#### injectInfiniteQueryFn
46β
47() => [`DefinedInitialDataInfiniteOptions`](../type-aliases/DefinedInitialDataInfiniteOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
48β
49A function returning the [DefinedInitialDataInfiniteOptions](../type-aliases/DefinedInitialDataInfiniteOptions.md) to use β
50everything you can pass to `injectInfiniteQuery`, with `initialData` set. Similar to `computed` from
51Angular, this function runs in the reactive context, so signals read inside it drive the query.
52β
53#### options?
54β
55[`InjectInfiniteQueryOptions`](../interfaces/InjectInfiniteQueryOptions.md)
56β
57Additional configuration.
58β
59### Returns
60β
61[`DefinedCreateInfiniteQueryResult`](../type-aliases/DefinedCreateInfiniteQueryResult.md)\<`TData`, `TError`\>
62β
63The same signals as `injectQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
64`hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data().pages` and
65`data().pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
66`InfiniteData<TQueryFnData>` shape.
67β
68### Remarks
69β
70Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
71refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user
72actions, or add conditions like `hasNextPage() && !isFetching()`.
73β
74### See
75β
76[infiniteQueryOptions](infiniteQueryOptions.md) to share these options between `injectInfiniteQuery` and imperative APIs
77like `queryClient.fetchInfiniteQuery`.
78β
79### Example
80β
81```angular-ts
82@Component({
83 selector: 'projects',
84 template: `
85 <!-- `projectsQuery.data()` is never `undefined`, thanks to `initialData` β even if a
86 refetch fails, so the list stays visible alongside the error. -->
87 <ul>
88 @for (page of projectsQuery.data().pages; track $index) {
89 @for (project of page.projects; track project.id) {
90 <li>{{ project.name }}</li>
91 }
92 }
93 </ul>
94 `,
95})
96export class Projects {
97 readonly projectsQuery = injectInfiniteQuery(() => ({
98 queryKey: ['projects'],
99 queryFn: ({ pageParam }) => fetchProjects(pageParam),
100 initialPageParam: 0,
101 getNextPageParam: (lastPage) => lastPage.nextId,
102 initialData: { pages: [], pageParams: [] },
103 }))
104}
105```
106β
107## Call Signature
108β
109```ts
110function injectInfiniteQuery<TQueryFnData, TError, TData, TQueryKey, TPageParam>(injectInfiniteQueryFn: () => UndefinedInitialDataInfiniteOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, options?: InjectInfiniteQueryOptions): CreateInfiniteQueryResult<TData, TError>;
111```
112β
113Defined in: [packages/angular-query-experimental/src/inject-infinite-query.ts:239](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-infinite-query.ts#L239)
114β
115Injects an infinite query: a declarative dependency on an asynchronous source of data that is tied to a
116unique key. Infinite queries can additively "load more" data onto an existing set of data, or
117"infinite scroll".
118β
119### Type Parameters
120β
121#### TQueryFnData
122β
123`TQueryFnData`
124β
125#### TError
126β
127`TError` = `Error`
128β
129#### TData
130β
131`TData` = [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `unknown`\>
132β
133#### TQueryKey
134β
135`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
136β
137#### TPageParam
138β
139`TPageParam` = `unknown`
140β
141### Parameters
142β
143#### injectInfiniteQueryFn
144β
145() => [`UndefinedInitialDataInfiniteOptions`](../type-aliases/UndefinedInitialDataInfiniteOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
146β
147A function returning the [UndefinedInitialDataInfiniteOptions](../type-aliases/UndefinedInitialDataInfiniteOptions.md) to use
148β everything you can pass to `injectInfiniteQuery`. Similar to `computed` from Angular, this function runs
149in the reactive context, so signals read inside it drive the query.
150β
151#### options?
152β
153[`InjectInfiniteQueryOptions`](../interfaces/InjectInfiniteQueryOptions.md)
154β
155Additional configuration.
156β
157### Returns
158β
159[`CreateInfiniteQueryResult`](../type-aliases/CreateInfiniteQueryResult.md)\<`TData`, `TError`\>
160β
161The same signals as `injectQuery`, with the addition of `fetchNextPage`, `fetchPreviousPage`,
162`hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and `isFetchingPreviousPage`. `data().pages` and
163`data().pageParams` are also added, as long as a `select` doesn't change `TData` away from its default
164`InfiniteData<TQueryFnData>` shape.
165β
166### Remarks
167β
168Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
169refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user
170actions, or add conditions like `hasNextPage() && !isFetching()`. This is the only overload that accepts
171`queryFn: skipToken`, shown below.
172β
173### See
174β
175[infiniteQueryOptions](infiniteQueryOptions.md) to share these options between `injectInfiniteQuery` and imperative APIs
176like `queryClient.fetchInfiniteQuery`.
177β
178### Examples
179β
180Fetching the next page from a button click:
181```angular-ts
182@Component({
183 selector: 'projects-list',
184 template: `
185 <ul>
186 @for (page of projectsQuery.data()?.pages; track $index) {
187 @for (project of page.projects; track project.id) {
188 <li>{{ project.name }}</li>
189 }
190 }
191 </ul>
192 <button
193 [disabled]="!projectsQuery.hasNextPage() || projectsQuery.isFetching()"
194 (click)="projectsQuery.fetchNextPage()"
195 >
196 Load More
197 </button>
198 `,
199})
200export class ProjectsList {
201 readonly projectsQuery = injectInfiniteQuery(() => ({
202 queryKey: ['projects'],
203 queryFn: ({ pageParam }) => fetchProjects(pageParam),
204 initialPageParam: 0,
205 getNextPageParam: (lastPage) => lastPage.nextId,
206 }))
207}
208```
209β
210Fetching the next page automatically as the user scrolls, using an `IntersectionObserver` on a sentinel
211element after the list:
212```angular-ts
213@Component({
214 selector: 'projects-list',
215 template: `
216 <ul>
217 @for (page of projectsQuery.data()?.pages; track $index) {
218 @for (project of page.projects; track project.id) {
219 <li>{{ project.name }}</li>
220 }
221 }
222 </ul>
223 <div #sentinel>{{ projectsQuery.isFetchingNextPage() ? 'Loading more...' : '' }}</div>
224 `,
225})
226export class ProjectsList {
227 readonly sentinel = viewChild<ElementRef<HTMLElement>>('sentinel')
228β
229 readonly projectsQuery = injectInfiniteQuery(() => ({
230 queryKey: ['projects'],
231 queryFn: ({ pageParam }) => fetchProjects(pageParam),
232 initialPageParam: 0,
233 getNextPageParam: (lastPage) => lastPage.nextId,
234 }))
235β
236 constructor() {
237 effect((onCleanup) => {
238 const sentinel = this.sentinel()?.nativeElement
239 if (
240 sentinel == null ||
241 !this.projectsQuery.hasNextPage() ||
242 this.projectsQuery.isFetching()
243 ) {
244 return
245 }
246β
247 const observer = new IntersectionObserver(([entry]) => {
248 if (entry?.isIntersecting) this.projectsQuery.fetchNextPage()
249 })
250 observer.observe(sentinel)
251β
252 onCleanup(() => observer.disconnect())
253 })
254 }
255}
256```
257β
258A query that's disabled, type safe, until `postId` is set β pass `skipToken` as `queryFn` instead of
259setting `enabled: false`:
260```angular-ts
261@Component({
262 selector: 'comments',
263 template: `
264 @if (postId() == null) {
265 Select a post
266 } @else if (commentsQuery.isPending()) {
267 Loading...
268 } @else if (commentsQuery.isError()) {
269 <span>Error: {{ commentsQuery.error()?.message }}</span>
270 } @else {
271 <ul>
272 @for (page of commentsQuery.data().pages; track $index) {
273 @for (comment of page.comments; track comment.id) {
274 <li>{{ comment.text }}</li>
275 }
276 }
277 </ul>
278 }
279 `,
280})
281export class Comments {
282 readonly postId = signal<string | undefined>(undefined)
283β
284 readonly commentsQuery = injectInfiniteQuery(() => ({
285 queryKey: ['post', this.postId(), 'comments'],
286 queryFn:
287 this.postId() != null
288 ? ({ pageParam }) => fetchComments(this.postId()!, pageParam)
289 : skipToken,
290 initialPageParam: 0,
291 getNextPageParam: (lastPage) => lastPage.nextId,
292 }))
293}
294```
295β
296## Call Signature
297β
298```ts
299function injectInfiniteQuery<TQueryFnData, TError, TData, TQueryKey, TPageParam>(injectInfiniteQueryFn: () => CreateInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, options?: InjectInfiniteQueryOptions): CreateInfiniteQueryResult<TData, TError>;
300```
301β
302Defined in: [packages/angular-query-experimental/src/inject-infinite-query.ts:267](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/inject-infinite-query.ts#L267)
303β
304This overload accepts the general [CreateInfiniteQueryOptions](../interfaces/CreateInfiniteQueryOptions.md) shape rather than the
305`initialData`-aware overloads above, so whether `data` is defined can't be inferred from the call site β
306useful when wrapping `injectInfiniteQuery` in your own helper function that forwards caller-provided
307options.
308β
309### Type Parameters
310β
311#### TQueryFnData
312β
313`TQueryFnData`
314β
315#### TError
316β
317`TError` = `Error`
318β
319#### TData
320β
321`TData` = [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `unknown`\>
322β
323#### TQueryKey
324β
325`TQueryKey` *extends* readonly `unknown`[] = readonly `unknown`[]
326β
327#### TPageParam
328β
329`TPageParam` = `unknown`
330β
331### Parameters
332β
333#### injectInfiniteQueryFn
334β
335() => [`CreateInfiniteQueryOptions`](../interfaces/CreateInfiniteQueryOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
336β
337A function that returns infinite query options. Similar to `computed` from
338Angular, this function runs in the reactive context, so signals read inside it drive the query.
339β
340#### options?
341β
342[`InjectInfiniteQueryOptions`](../interfaces/InjectInfiniteQueryOptions.md)
343β
344Additional configuration.
345β
346### Returns
347β
348[`CreateInfiniteQueryResult`](../type-aliases/CreateInfiniteQueryResult.md)\<`TData`, `TError`\>
349β
350The infinite query result.