# InfiniteQueryObserver

Defined in: [packages/query-core/src/infiniteQueryObserver.ts:40](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L40)

An `InfiniteQueryObserver` extends `QueryObserver` to observe and switch
between infinite queries. It augments the base `QueryObserverResult` with
infinite-query-specific fields and methods, such as `hasNextPage` and
`fetchNextPage`, and is the primitive that framework adapters (e.g.
`useInfiniteQuery`) build their hooks on top of.

## Example

```ts
const observer = new InfiniteQueryObserver(queryClient, {
  queryKey: ['projects'],
  queryFn: ({ pageParam }) => fetchProjects(pageParam),
  initialPageParam: 0,
  getNextPageParam: (lastPage) => lastPage.nextCursor,
})

const unsubscribe = observer.subscribe((result) => console.log(result))
```

## Extends

- [`QueryObserver`](QueryObserver.md)\<`TQueryFnData`, `TError`, `TData`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\>, `TQueryKey`\>

## Type Parameters

### TQueryFnData

`TQueryFnData` = `unknown`

### TError

`TError` = [`DefaultError`](../type-aliases/DefaultError.md)

### TData

`TData` = [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`\>

### TQueryKey

`TQueryKey` *extends* [`QueryKey`](../type-aliases/QueryKey.md) = [`QueryKey`](../type-aliases/QueryKey.md)

### TPageParam

`TPageParam` = `unknown`

## Constructors

### Constructor

```ts
new InfiniteQueryObserver<TQueryFnData, TError, TData, TQueryKey, TPageParam>(client: QueryClient, options: InfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>): InfiniteQueryObserver<TQueryFnData, TError, TData, TQueryKey, TPageParam>;
```

Defined in: [packages/query-core/src/infiniteQueryObserver.ts:82](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L82)

#### Parameters

##### client

[`QueryClient`](QueryClient.md)

##### options

[`InfiniteQueryObserverOptions`](../interfaces/InfiniteQueryObserverOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>

#### Returns

`InfiniteQueryObserver`\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>

#### Overrides

[`QueryObserver`](QueryObserver.md).[`constructor`](QueryObserver.md#constructor)

## Properties

### getCurrentResult

```ts
getCurrentResult: ReplaceReturnType<() => QueryObserverResult<TData, TError>, InfiniteQueryObserverResult<TData, TError>>;
```

Defined in: [packages/query-core/src/infiniteQueryObserver.ts:59](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L59)

Returns the most recently computed `QueryObserverResult` for the
observed query. This is a point-in-time read; to be notified of updates
as they happen, subscribe to the observer instead (its inherited
`subscribe` method).

#### Returns

The observer's latest result.

#### Example

```ts
const result = observer.getCurrentResult()
console.log(result.status, result.data)
```

#### Overrides

```ts
QueryObserver.getCurrentResult
```

***

### options

```ts
options: QueryObserverOptions<TQueryFnData, TError, TData, InfiniteData<TQueryFnData, TPageParam>, TQueryKey>;
```

Defined in: [packages/query-core/src/queryObserver.ts:88](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L88)

#### Inherited from

[`QueryObserver`](QueryObserver.md).[`options`](QueryObserver.md#options)

***

### subscribe()

```ts
subscribe: (listener: InfiniteQueryObserverListener) => () => void;
```

Defined in: [packages/query-core/src/infiniteQueryObserver.ts:54](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L54)

Registers a listener to be called on every update this object notifies about. Returns a function
that removes the listener again — call it to stop listening. The base class never drops a listener
on its own, though some subclasses clear all of theirs in `destroy()`.

#### Parameters

##### listener

`InfiniteQueryObserverListener`

Called on each update, with whatever the subclass passes to its subscribers.

#### Returns

A function that removes the listener.

```ts
(): void;
```

##### Returns

`void`

#### Example

```ts
const unsubscribe = subscribable.subscribe(() => {
  // react to the update
})

unsubscribe()
```

#### Overrides

```ts
QueryObserver.subscribe
```

## Methods

### destroy()

```ts
destroy(): void;
```

Defined in: [packages/query-core/src/queryObserver.ts:162](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L162)

Stops observing the current query: clears all listeners, cancels the
stale and refetch-interval timers, and removes this observer from the
query it was observing.

#### Returns

`void`

#### Inherited from

[`QueryObserver`](QueryObserver.md).[`destroy`](QueryObserver.md#destroy)

***

### fetchNextPage()

```ts
fetchNextPage(options?: FetchNextPageOptions): Promise<InfiniteQueryObserverResult<TData, TError>>;
```

Defined in: [packages/query-core/src/infiniteQueryObserver.ts:165](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L165)

Fetches the next page of the infinite query and returns a promise that
resolves with the resulting `InfiniteQueryObserverResult`. The page
param used for the fetch is determined by `getNextPageParam`, which
receives the current pages/page params and whose result also determines
`hasNextPage`.

#### Parameters

##### options?

[`FetchNextPageOptions`](../interfaces/FetchNextPageOptions.md)

Set `cancelRefetch` to `false` to ignore the call while a fetch is running,
and `throwOnError` to `true` to reject when the fetch fails.

#### Returns

`Promise`\<[`InfiniteQueryObserverResult`](../type-aliases/InfiniteQueryObserverResult.md)\<`TData`, `TError`\>\>

A promise that resolves with the result after the next page is fetched. With
`cancelRefetch: false`, a running fetch is reused instead, so the next page may not be fetched.

#### See

[InfiniteQueryObserver#fetchPreviousPage](#fetchpreviouspage)

#### Example

```ts
const { hasNextPage } = observer.getCurrentResult()

if (hasNextPage) {
  await observer.fetchNextPage()
}
```

***

### fetchOptimistic()

```ts
fetchOptimistic(options: QueryObserverOptions<TQueryFnData, TError, TData, InfiniteData<TQueryFnData, TPageParam>, TQueryKey>): Promise<QueryObserverResult<TData, TError>>;
```

Defined in: [packages/query-core/src/queryObserver.ts:408](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L408)

Fetches a query defined by the given options without affecting this
observer's own tracked query or result, and returns a promise that
resolves with the `QueryObserverResult` for that fetch. This is useful
for prefetching data that another observer (e.g. a query about to be
navigated to) will need, ahead of time.

#### Parameters

##### options

[`QueryObserverOptions`](../interfaces/QueryObserverOptions.md)\<`TQueryFnData`, `TError`, `TData`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\>, `TQueryKey`\>

The observer options of the query to fetch.

#### Returns

`Promise`\<[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<`TData`, `TError`\>\>

A promise that resolves with the result for the fetched query.

#### Example

```ts
const result = await observer.fetchOptimistic({
  queryKey: ['posts', 2],
  queryFn: () => fetchPost(2),
})
console.log(result.data)
```

#### Inherited from

[`QueryObserver`](QueryObserver.md).[`fetchOptimistic`](QueryObserver.md#fetchoptimistic)

***

### fetchPreviousPage()

```ts
fetchPreviousPage(options?: FetchPreviousPageOptions): Promise<InfiniteQueryObserverResult<TData, TError>>;
```

Defined in: [packages/query-core/src/infiniteQueryObserver.ts:196](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L196)

Fetches the previous page of the infinite query and returns a promise
that resolves with the resulting `InfiniteQueryObserverResult`. The page
param used for the fetch is determined by `getPreviousPageParam`, which
receives the current pages/page params and whose result also determines
`hasPreviousPage`.

#### Parameters

##### options?

[`FetchPreviousPageOptions`](../interfaces/FetchPreviousPageOptions.md)

Set `cancelRefetch` to `false` to ignore the call while a fetch is running,
and `throwOnError` to `true` to reject when the fetch fails.

#### Returns

`Promise`\<[`InfiniteQueryObserverResult`](../type-aliases/InfiniteQueryObserverResult.md)\<`TData`, `TError`\>\>

A promise that resolves with the result after the previous page is fetched. With
`cancelRefetch: false`, a running fetch is reused instead, so the previous page may not be fetched.

#### See

[InfiniteQueryObserver#fetchNextPage](#fetchnextpage)

#### Example

```ts
const { hasPreviousPage } = observer.getCurrentResult()

if (hasPreviousPage) {
  await observer.fetchPreviousPage()
}
```

***

### getCurrentQuery()

```ts
getCurrentQuery(): Query<TQueryFnData, TError, InfiniteData<TQueryFnData, TPageParam>, TQueryKey>;
```

Defined in: [packages/query-core/src/queryObserver.ts:367](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L367)

Returns the `Query` instance this observer is currently observing.

#### Returns

[`Query`](Query.md)\<`TQueryFnData`, `TError`, [`InfiniteData`](../interfaces/InfiniteData.md)\<`TQueryFnData`, `TPageParam`\>, `TQueryKey`\>

The observed query.

#### Inherited from

[`QueryObserver`](QueryObserver.md).[`getCurrentQuery`](QueryObserver.md#getcurrentquery)

***

### getOptimisticResult()

```ts
getOptimisticResult(options: DefaultedInfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>): InfiniteQueryObserverResult<TData, TError>;
```

Defined in: [packages/query-core/src/infiniteQueryObserver.ts:129](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L129)

The infinite-query counterpart of [QueryObserver#getOptimisticResult](QueryObserver.md#getoptimisticresult), marking the
options as an infinite query before delegating to it. Called by framework adapters (e.g.
`useInfiniteQuery`) ahead of subscribing, to compute the current `InfiniteQueryObserverResult`
synchronously.

#### Parameters

##### options

[`DefaultedInfiniteQueryObserverOptions`](../type-aliases/DefaultedInfiniteQueryObserverOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>

The defaulted infinite query observer options to compute the result for.

#### Returns

[`InfiniteQueryObserverResult`](../type-aliases/InfiniteQueryObserverResult.md)\<`TData`, `TError`\>

The result for the given options.

#### Overrides

[`QueryObserver`](QueryObserver.md).[`getOptimisticResult`](QueryObserver.md#getoptimisticresult)

***

### hasListeners()

```ts
hasListeners(): boolean;
```

Defined in: [packages/query-core/src/subscribable.ts:43](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L43)

Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.

#### Returns

`boolean`

`true` if at least one listener is registered.

#### Inherited from

[`QueryObserver`](QueryObserver.md).[`hasListeners`](QueryObserver.md#haslisteners)

***

### refetch()

```ts
refetch(options: RefetchOptions): Promise<QueryObserverResult<TData, TError>>;
```

Defined in: [packages/query-core/src/queryObserver.ts:383](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L383)

Refetches the observed query and returns a promise that resolves with
the resulting `QueryObserverResult`.

#### Parameters

##### options

[`RefetchOptions`](../interfaces/RefetchOptions.md) = `{}`

Set `cancelRefetch` to `false` to keep a running fetch instead of cancelling
it, and `throwOnError` to `true` to reject when the refetch fails.

#### Returns

`Promise`\<[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<`TData`, `TError`\>\>

A promise that resolves with the result after the refetch.

#### Example

```ts
const result = await observer.refetch({ cancelRefetch: false })
console.log(result.data)
```

#### Inherited from

[`QueryObserver`](QueryObserver.md).[`refetch`](QueryObserver.md#refetch)

***

### setOptions()

```ts
setOptions(options: InfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>): void;
```

Defined in: [packages/query-core/src/infiniteQueryObserver.ts:108](https://github.com/TanStack/query/blob/main/packages/query-core/src/infiniteQueryObserver.ts#L108)

Updates the observer's options. Behaves the same as
`QueryObserver.setOptions`, additionally marking the options as
belonging to an infinite query before delegating to the base
implementation.

#### Parameters

##### options

[`InfiniteQueryObserverOptions`](../interfaces/InfiniteQueryObserverOptions.md)\<`TQueryFnData`, `TError`, `TData`, `TQueryKey`, `TPageParam`\>

The new infinite query observer options.

#### Returns

`void`

#### Overrides

[`QueryObserver`](QueryObserver.md).[`setOptions`](QueryObserver.md#setoptions)

***

### shouldFetchOnReconnect()

```ts
shouldFetchOnReconnect(): boolean;
```

Defined in: [packages/query-core/src/queryObserver.ts:135](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L135)

Returns whether the observed query is currently stale and configured
(via the `refetchOnReconnect` option) to refetch when the network
reconnects.

#### Returns

`boolean`

`true` if the observer should refetch the query on reconnect.

#### Inherited from

[`QueryObserver`](QueryObserver.md).[`shouldFetchOnReconnect`](QueryObserver.md#shouldfetchonreconnect)

***

### shouldFetchOnWindowFocus()

```ts
shouldFetchOnWindowFocus(): boolean;
```

Defined in: [packages/query-core/src/queryObserver.ts:149](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L149)

Returns whether the observed query is currently stale and configured
(via the `refetchOnWindowFocus` option) to refetch when the window
regains focus.

#### Returns

`boolean`

`true` if the observer should refetch the query on window focus.

#### Inherited from

[`QueryObserver`](QueryObserver.md).[`shouldFetchOnWindowFocus`](QueryObserver.md#shouldfetchonwindowfocus)

***

### trackProp()

```ts
trackProp(key: 
  | "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"): void;
```

Defined in: [packages/query-core/src/queryObserver.ts:359](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L359)

Records that the given `QueryObserverResult` property was read, so a subsequent update only
notifies this observer if a tracked property actually changed. Normally called indirectly via
[QueryObserver#trackResult](QueryObserver.md#trackresult)'s proxy; exposed directly for adapters that track property
access themselves (e.g. through their own reactivity system) instead of via the proxy.

#### Parameters

##### key

The name of the property that was read.

`"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"`

#### Returns

`void`

#### Inherited from

[`QueryObserver`](QueryObserver.md).[`trackProp`](QueryObserver.md#trackprop)

***

### trackResult()

```ts
trackResult(result: QueryObserverResult<TData, TError>, onPropTracked?: (key: 
  | "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") => void): QueryObserverResult<TData, TError>;
```

Defined in: [packages/query-core/src/queryObserver.ts:339](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L339)

Wraps a `QueryObserverResult` in a `Proxy` that records which properties are read, via
[QueryObserver#trackProp](QueryObserver.md#trackprop) (and an optional `onPropTracked` callback). Used by framework
adapters when `notifyOnChangeProps` is not set, to implement its default "only re-render on
properties you actually read" behavior.

#### Parameters

##### result

[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<`TData`, `TError`\>

The result to wrap.

##### onPropTracked?

(`key`: 
  \| `"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"`) => `void`

Called with the name of each property that is read.

#### Returns

[`QueryObserverResult`](../type-aliases/QueryObserverResult.md)\<`TData`, `TError`\>

A proxy of `result` that tracks property reads.

#### Inherited from

[`QueryObserver`](QueryObserver.md).[`trackResult`](QueryObserver.md#trackresult)

***

### updateResult()

```ts
updateResult(): void;
```

Defined in: [packages/query-core/src/queryObserver.ts:746](https://github.com/TanStack/query/blob/main/packages/query-core/src/queryObserver.ts#L746)

Recomputes and stores the current result from the current query/options, notifying listeners
if it changed. Framework adapters call this right after subscribing to make sure no query
update was missed in the gap between creating the observer and subscribing to it.

#### Returns

`void`

#### Inherited from

[`QueryObserver`](QueryObserver.md).[`updateResult`](QueryObserver.md#updateresult)
