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