Open History to see the last commit
1---
2id: InfiniteQueryObserver
3title: InfiniteQueryObserver
4---
5β
6Defined in: [packages/query-core/src/infiniteQueryObserver.ts:40](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L40)
7β
8An `InfiniteQueryObserver` extends `QueryObserver` to observe and switch
9between infinite queries. It augments the base `QueryObserverResult` with
10infinite-query-specific fields and methods, such as `hasNextPage` and
11`fetchNextPage`, and is the primitive that framework adapters (e.g.
12`useInfiniteQuery`) build their hooks on top of.
13β
14## Example
15β
16```ts
17const observer = new InfiniteQueryObserver(queryClient, {
18 queryKey: ['projects'],
19 queryFn: ({ pageParam }) => fetchProjects(pageParam),
20 initialPageParam: 0,
21 getNextPageParam: (lastPage) => lastPage.nextCursor,
22})
23β
24const unsubscribe = observer.subscribe((result) => console.log(result))
25```
26β
27## Extends
28β
29- [`QueryObserver`](QueryObserver.md)\<`TQueryFnData`, `TError`, `TData`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\>, `TQueryKey`\>
30β
31## Type Parameters
32β
33### TQueryFnData
34β
35`TQueryFnData` = `unknown`
36β
37### TError
38β
39`TError` = [`DefaultError`](../type-aliases/DefaultError.md)
40β
41### TData
42β
43`TData` = [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`\>
44β
45### TQueryKey
46β
47`TQueryKey` *extends* [`QueryKey`](../type-aliases/QueryKey.md) = [`QueryKey`](../type-aliases/QueryKey.md)
48β
49### TPageParam
50β
51`TPageParam` = `unknown`
52β
53## Constructors
54β
55### Constructor
56β
57```ts
58new InfiniteQueryObserver<TQueryFnData, TError, TData, TQueryKey, TPageParam>(client: QueryClient, options: InfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>): InfiniteQueryObserver<TQueryFnData, TError, TData, TQueryKey, TPageParam>;
59```
60β
61Defined in: [packages/query-core/src/infiniteQueryObserver.ts:82](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L82)
62β
63#### Parameters
64β
65##### client
66β
67`QueryClient`
68β
69##### options
70β
71`InfiniteQueryObserverOptions`\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
72β
73#### Returns
74β
75`InfiniteQueryObserver`\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
76β
77#### Overrides
78β
79[`QueryObserver`](QueryObserver.md).[`constructor`](QueryObserver.md#constructor)
80β
81## Properties
82β
83### getCurrentResult
84β
85```ts
86getCurrentResult: ReplaceReturnType<() => QueryObserverResult<TData, TError>, InfiniteQueryObserverResult<TData, TError>>;
87```
88β
89Defined in: [packages/query-core/src/infiniteQueryObserver.ts:59](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L59)
90β
91Returns the most recently computed `QueryObserverResult` for the
92observed query. This is a point-in-time read; to be notified of updates
93as they happen, subscribe to the observer instead (its inherited
94`subscribe` method).
95β
96#### Returns
97β
98The observer's latest result.
99β
100#### Example
101β
102```ts
103const result = observer.getCurrentResult()
104console.log(result.status, result.data)
105```
106β
107#### Overrides
108β
109```ts
110QueryObserver.getCurrentResult
111```
112β
113***
114β
115### options
116β
117```ts
118options: QueryObserverOptions<TQueryFnData, TError, TData, InfiniteData<TQueryFnData, TPageParam>, TQueryKey>;
119```
120β
121Defined in: [packages/query-core/src/queryObserver.ts:88](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L88)
122β
123#### Inherited from
124β
125[`QueryObserver`](QueryObserver.md).[`options`](QueryObserver.md#options)
126β
127***
128β
129### subscribe
130β
131```ts
132subscribe: (listener: InfiniteQueryObserverListener) => () => void;
133```
134β
135Defined in: [packages/query-core/src/infiniteQueryObserver.ts:54](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L54)
136β
137Registers a listener to be called on every update this object notifies about. Returns a function
138that removes the listener again β call it to stop listening. The base class never drops a listener
139on its own, though some subclasses clear all of theirs in `destroy()`.
140β
141#### Parameters
142β
143##### listener
144β
145`InfiniteQueryObserverListener`
146β
147Called on each update, with whatever the subclass passes to its subscribers.
148β
149#### Returns
150β
151A function that removes the listener.
152β
153() => `void`
154β
155#### Example
156β
157```ts
158const unsubscribe = subscribable.subscribe(() => {
159 // react to the update
160})
161β
162unsubscribe()
163```
164β
165#### Overrides
166β
167```ts
168QueryObserver.subscribe
169```
170β
171## Methods
172β
173### destroy()
174β
175```ts
176destroy(): void;
177```
178β
179Defined in: [packages/query-core/src/queryObserver.ts:166](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L166)
180β
181Stops observing the current query: clears all listeners, cancels the
182stale and refetch-interval timers, and removes this observer from the
183query it was observing.
184β
185#### Returns
186β
187`void`
188β
189#### Inherited from
190β
191[`QueryObserver`](QueryObserver.md).[`destroy`](QueryObserver.md#destroy)
192β
193***
194β
195### fetchNextPage()
196β
197```ts
198fetchNextPage(options?: FetchNextPageOptions): Promise<InfiniteQueryObserverResult<TData, TError>>;
199```
200β
201Defined in: [packages/query-core/src/infiniteQueryObserver.ts:165](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L165)
202β
203Fetches the next page of the infinite query and returns a promise that
204resolves with the resulting `InfiniteQueryObserverResult`. The page
205param used for the fetch is determined by `getNextPageParam`, which
206receives the current pages/page params and whose result also determines
207`hasNextPage`.
208β
209#### Parameters
210β
211##### options?
212β
213[`FetchNextPageOptions`](../interfaces/FetchNextPageOptions.md)
214β
215Set `cancelRefetch` to `false` to ignore the call while a fetch is running,
216and `throwOnError` to `true` to reject when the fetch fails.
217β
218#### Returns
219β
220`Promise`\<[`InfiniteQueryObserverResult`](../type-aliases/InfiniteQueryObserverResult.md)\<`TData`, `TError`\>\>
221β
222A promise that resolves with the result after the next page is fetched. With
223`cancelRefetch: false`, a running fetch is reused instead, so the next page may not be fetched.
224β
225#### See
226β
227[InfiniteQueryObserver#fetchPreviousPage](#fetchpreviouspage)
228β
229#### Example
230β
231```ts
232const { hasNextPage } = observer.getCurrentResult()
233β
234if (hasNextPage) {
235 await observer.fetchNextPage()
236}
237```
238β
239***
240β
241### fetchOptimistic()
242β
243```ts
244fetchOptimistic(options: QueryObserverOptions<TQueryFnData, TError, TData, InfiniteData<TQueryFnData, TPageParam>, TQueryKey>): Promise<QueryObserverResult<TData, TError>>;
245```
246β
247Defined in: [packages/query-core/src/queryObserver.ts:412](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L412)
248β
249Fetches a query defined by the given options without affecting this
250observer's own tracked query or result, and returns a promise that
251resolves with the `QueryObserverResult` for that fetch. This is useful
252for prefetching data that another observer (e.g. a query about to be
253navigated to) will need, ahead of time.
254β
255#### Parameters
256β
257##### options
258β
259`QueryObserverOptions`\<`TQueryFnData`, `TError`, `TData`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\>, `TQueryKey`\>
260β
261The observer options of the query to fetch.
262β
263#### Returns
264β
265`Promise`\<[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<`TData`, `TError`\>\>
266β
267A promise that resolves with the result for the fetched query.
268β
269#### Example
270β
271```ts
272const result = await observer.fetchOptimistic({
273 queryKey: ['posts', 2],
274 queryFn: () => fetchPost(2),
275})
276console.log(result.data)
277```
278β
279#### Inherited from
280β
281[`QueryObserver`](QueryObserver.md).[`fetchOptimistic`](QueryObserver.md#fetchoptimistic)
282β
283***
284β
285### fetchPreviousPage()
286β
287```ts
288fetchPreviousPage(options?: FetchPreviousPageOptions): Promise<InfiniteQueryObserverResult<TData, TError>>;
289```
290β
291Defined in: [packages/query-core/src/infiniteQueryObserver.ts:196](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L196)
292β
293Fetches the previous page of the infinite query and returns a promise
294that resolves with the resulting `InfiniteQueryObserverResult`. The page
295param used for the fetch is determined by `getPreviousPageParam`, which
296receives the current pages/page params and whose result also determines
297`hasPreviousPage`.
298β
299#### Parameters
300β
301##### options?
302β
303[`FetchPreviousPageOptions`](../interfaces/FetchPreviousPageOptions.md)
304β
305Set `cancelRefetch` to `false` to ignore the call while a fetch is running,
306and `throwOnError` to `true` to reject when the fetch fails.
307β
308#### Returns
309β
310`Promise`\<[`InfiniteQueryObserverResult`](../type-aliases/InfiniteQueryObserverResult.md)\<`TData`, `TError`\>\>
311β
312A promise that resolves with the result after the previous page is fetched. With
313`cancelRefetch: false`, a running fetch is reused instead, so the previous page may not be fetched.
314β
315#### See
316β
317[InfiniteQueryObserver#fetchNextPage](#fetchnextpage)
318β
319#### Example
320β
321```ts
322const { hasPreviousPage } = observer.getCurrentResult()
323β
324if (hasPreviousPage) {
325 await observer.fetchPreviousPage()
326}
327```
328β
329***
330β
331### getCurrentQuery()
332β
333```ts
334getCurrentQuery(): Query<TQueryFnData, TError, InfiniteData<TQueryFnData, TPageParam>, TQueryKey>;
335```
336β
337Defined in: [packages/query-core/src/queryObserver.ts:371](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L371)
338β
339Returns the `Query` instance this observer is currently observing.
340β
341#### Returns
342β
343[`Query`](Query.md)\<`TQueryFnData`, `TError`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\>, `TQueryKey`\>
344β
345The observed query.
346β
347#### Inherited from
348β
349[`QueryObserver`](QueryObserver.md).[`getCurrentQuery`](QueryObserver.md#getcurrentquery)
350β
351***
352β
353### getOptimisticResult()
354β
355```ts
356getOptimisticResult(options: DefaultedInfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>): InfiniteQueryObserverResult<TData, TError>;
357```
358β
359Defined in: [packages/query-core/src/infiniteQueryObserver.ts:129](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L129)
360β
361The infinite-query counterpart of [QueryObserver#getOptimisticResult](QueryObserver.md#getoptimisticresult), marking the
362options as an infinite query before delegating to it. Called by framework adapters (e.g.
363`useInfiniteQuery`) ahead of subscribing, to compute the current `InfiniteQueryObserverResult`
364synchronously.
365β
366#### Parameters
367β
368##### options
369β
370[`DefaultedInfiniteQueryObserverOptions`](../type-aliases/DefaultedInfiniteQueryObserverOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
371β
372The defaulted infinite query observer options to compute the result for.
373β
374#### Returns
375β
376[`InfiniteQueryObserverResult`](../type-aliases/InfiniteQueryObserverResult.md)\<`TData`, `TError`\>
377β
378The result for the given options.
379β
380#### Overrides
381β
382[`QueryObserver`](QueryObserver.md).[`getOptimisticResult`](QueryObserver.md#getoptimisticresult)
383β
384***
385β
386### hasListeners()
387β
388```ts
389hasListeners(): boolean;
390```
391β
392Defined in: [packages/query-core/src/subscribable.ts:43](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L43)
393β
394Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
395β
396#### Returns
397β
398`boolean`
399β
400`true` if at least one listener is registered.
401β
402#### Inherited from
403β
404[`QueryObserver`](QueryObserver.md).[`hasListeners`](QueryObserver.md#haslisteners)
405β
406***
407β
408### refetch()
409β
410```ts
411refetch(options?: RefetchOptions): Promise<QueryObserverResult<TData, TError>>;
412```
413β
414Defined in: [packages/query-core/src/queryObserver.ts:387](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L387)
415β
416Refetches the observed query and returns a promise that resolves with
417the resulting `QueryObserverResult`.
418β
419#### Parameters
420β
421##### options?
422β
423[`RefetchOptions`](../interfaces/RefetchOptions.md) = `{}`
424β
425Set `cancelRefetch` to `false` to keep a running fetch instead of cancelling
426it, and `throwOnError` to `true` to reject when the refetch fails.
427β
428#### Returns
429β
430`Promise`\<[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<`TData`, `TError`\>\>
431β
432A promise that resolves with the result after the refetch.
433β
434#### Example
435β
436```ts
437const result = await observer.refetch({ cancelRefetch: false })
438console.log(result.data)
439```
440β
441#### Inherited from
442β
443[`QueryObserver`](QueryObserver.md).[`refetch`](QueryObserver.md#refetch)
444β
445***
446β
447### setOptions()
448β
449```ts
450setOptions(options: InfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>): void;
451```
452β
453Defined in: [packages/query-core/src/infiniteQueryObserver.ts:108](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L108)
454β
455Updates the observer's options. Behaves the same as
456`QueryObserver.setOptions`, additionally marking the options as
457belonging to an infinite query before delegating to the base
458implementation.
459β
460#### Parameters
461β
462##### options
463β
464`InfiniteQueryObserverOptions`\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>
465β
466The new infinite query observer options.
467β
468#### Returns
469β
470`void`
471β
472#### Overrides
473β
474[`QueryObserver`](QueryObserver.md).[`setOptions`](QueryObserver.md#setoptions)
475β
476***
477β
478### shouldFetchOnReconnect()
479β
480```ts
481shouldFetchOnReconnect(): boolean;
482```
483β
484Defined in: [packages/query-core/src/queryObserver.ts:139](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L139)
485β
486Returns whether the observed query is currently stale and configured
487(via the `refetchOnReconnect` option) to refetch when the network
488reconnects.
489β
490#### Returns
491β
492`boolean`
493β
494`true` if the observer should refetch the query on reconnect.
495β
496#### Inherited from
497β
498[`QueryObserver`](QueryObserver.md).[`shouldFetchOnReconnect`](QueryObserver.md#shouldfetchonreconnect)
499β
500***
501β
502### shouldFetchOnWindowFocus()
503β
504```ts
505shouldFetchOnWindowFocus(): boolean;
506```
507β
508Defined in: [packages/query-core/src/queryObserver.ts:153](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L153)
509β
510Returns whether the observed query is currently stale and configured
511(via the `refetchOnWindowFocus` option) to refetch when the window
512regains focus.
513β
514#### Returns
515β
516`boolean`
517β
518`true` if the observer should refetch the query on window focus.
519β
520#### Inherited from
521β
522[`QueryObserver`](QueryObserver.md).[`shouldFetchOnWindowFocus`](QueryObserver.md#shouldfetchonwindowfocus)
523β
524***
525β
526### trackProp()
527β
528```ts
529trackProp(key:
530 | "error"
531 | "data"
532 | "isError"
533 | "isPending"
534 | "isLoading"
535 | "isLoadingError"
536 | "isRefetchError"
537 | "isSuccess"
538 | "isPlaceholderData"
539 | "status"
540 | "dataUpdatedAt"
541 | "errorUpdatedAt"
542 | "failureCount"
543 | "failureReason"
544 | "errorUpdateCount"
545 | "isFetched"
546 | "isFetchedAfterMount"
547 | "isFetching"
548 | "isInitialLoading"
549 | "isPaused"
550 | "isRefetching"
551 | "isStale"
552 | "isEnabled"
553 | "refetch"
554 | "fetchStatus"): void;
555```
556β
557Defined in: [packages/query-core/src/queryObserver.ts:363](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L363)
558β
559Records that the given `QueryObserverResult` property was read, so a subsequent update only
560notifies this observer if a tracked property actually changed. Normally called indirectly via
561[QueryObserver#trackResult](QueryObserver.md#trackresult)'s proxy; exposed directly for adapters that track property
562access themselves (e.g. through their own reactivity system) instead of via the proxy.
563β
564#### Parameters
565β
566##### key
567β
568 \| `"error"`
569 \| `"data"`
570 \| `"isError"`
571 \| `"isPending"`
572 \| `"isLoading"`
573 \| `"isLoadingError"`
574 \| `"isRefetchError"`
575 \| `"isSuccess"`
576 \| `"isPlaceholderData"`
577 \| `"status"`
578 \| `"dataUpdatedAt"`
579 \| `"errorUpdatedAt"`
580 \| `"failureCount"`
581 \| `"failureReason"`
582 \| `"errorUpdateCount"`
583 \| `"isFetched"`
584 \| `"isFetchedAfterMount"`
585 \| `"isFetching"`
586 \| `"isInitialLoading"`
587 \| `"isPaused"`
588 \| `"isRefetching"`
589 \| `"isStale"`
590 \| `"isEnabled"`
591 \| `"refetch"`
592 \| `"fetchStatus"`
593β
594The name of the property that was read.
595β
596#### Returns
597β
598`void`
599β
600#### Inherited from
601β
602[`QueryObserver`](QueryObserver.md).[`trackProp`](QueryObserver.md#trackprop)
603β
604***
605β
606### trackResult()
607β
608```ts
609trackResult(result: QueryObserverResult<TData, TError>, onPropTracked?: (key:
610 | "error"
611 | "data"
612 | "isError"
613 | "isPending"
614 | "isLoading"
615 | "isLoadingError"
616 | "isRefetchError"
617 | "isSuccess"
618 | "isPlaceholderData"
619 | "status"
620 | "dataUpdatedAt"
621 | "errorUpdatedAt"
622 | "failureCount"
623 | "failureReason"
624 | "errorUpdateCount"
625 | "isFetched"
626 | "isFetchedAfterMount"
627 | "isFetching"
628 | "isInitialLoading"
629 | "isPaused"
630 | "isRefetching"
631 | "isStale"
632 | "isEnabled"
633 | "refetch"
634| "fetchStatus") => void): QueryObserverResult<TData, TError>;
635```
636β
637Defined in: [packages/query-core/src/queryObserver.ts:343](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L343)
638β
639Wraps a `QueryObserverResult` in a `Proxy` that records which properties are read, via
640[QueryObserver#trackProp](QueryObserver.md#trackprop) (and an optional `onPropTracked` callback). Used by framework
641adapters when `notifyOnChangeProps` is not set, to implement its default "only re-render on
642properties you actually read" behavior.
643β
644#### Parameters
645β
646##### result
647β
648[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<`TData`, `TError`\>
649β
650The result to wrap.
651β
652##### onPropTracked?
653β
654(`key`:
655 \| `"error"`
656 \| `"data"`
657 \| `"isError"`
658 \| `"isPending"`
659 \| `"isLoading"`
660 \| `"isLoadingError"`
661 \| `"isRefetchError"`
662 \| `"isSuccess"`
663 \| `"isPlaceholderData"`
664 \| `"status"`
665 \| `"dataUpdatedAt"`
666 \| `"errorUpdatedAt"`
667 \| `"failureCount"`
668 \| `"failureReason"`
669 \| `"errorUpdateCount"`
670 \| `"isFetched"`
671 \| `"isFetchedAfterMount"`
672 \| `"isFetching"`
673 \| `"isInitialLoading"`
674 \| `"isPaused"`
675 \| `"isRefetching"`
676 \| `"isStale"`
677 \| `"isEnabled"`
678 \| `"refetch"`
679 \| `"fetchStatus"`) => `void`
680β
681Called with the name of each property that is read.
682β
683#### Returns
684β
685[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<`TData`, `TError`\>
686β
687A proxy of `result` that tracks property reads.
688β
689#### Inherited from
690β
691[`QueryObserver`](QueryObserver.md).[`trackResult`](QueryObserver.md#trackresult)
692β
693***
694β
695### updateResult()
696β
697```ts
698updateResult(): void;
699```
700β
701Defined in: [packages/query-core/src/queryObserver.ts:765](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L765)
702β
703Recomputes and stores the current result from the current query/options, notifying listeners
704if it changed. Framework adapters call this right after subscribing to make sure no query
705update was missed in the gap between creating the observer and subscribing to it.
706β
707#### Returns
708β
709`void`
710β
711#### Inherited from
712β
713[`QueryObserver`](QueryObserver.md).[`updateResult`](QueryObserver.md#updateresult)