Skip to content
0

katanakit-js / InfiniteQueryObserver

Class: InfiniteQueryObserver<TQueryFnData, TError, TData, TQueryKey, TPageParam> ​

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/infiniteQueryObserver.d.ts:24

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 ​

Type Parameters ​

Type ParameterDefault type
TQueryFnDataunknown
TErrorDefaultError
TDataInfiniteData<TQueryFnData>
TQueryKey extends QueryKeyQueryKey
TPageParamunknown

Constructors ​

Constructor ​

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

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/infiniteQueryObserver.d.ts:28

Parameters ​

ParameterType
clientQueryClient
optionsInfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>

Returns ​

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

Overrides ​

QueryObserver.constructor

Properties ​

PropertyModifierTypeDescriptionOverridesInherited fromDefined in
fetchprotectedReplaceReturnType<(fetchOptions) => Promise<QueryObserverResult<TData, TError>>, Promise<InfiniteQueryObserverResult<TData, TError>>>-QueryObserver.fetch-node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/infiniteQueryObserver.d.ts:27
getCurrentResultpublicReplaceReturnType<() => QueryObserverResult<TData, TError>, InfiniteQueryObserverResult<TData, TError>>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). Example const result = observer.getCurrentResult() console.log(result.status, result.data)QueryObserver.getCurrentResult-node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/infiniteQueryObserver.d.ts:26
listenersprotectedSet<QueryObserverListener<TData, TError>>--QueryObserver.listenersnode_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/subscribable-CysQ0tne.d.ts:8
optionspublicQueryObserverOptions<TQueryFnData, TError, TData, InfiniteData<TQueryFnData, TPageParam>, TQueryKey>--QueryObserver.optionsnode_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:30
subscribepublic(listener) => () => voidRegisters 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(). Example const unsubscribe = subscribable.subscribe(() => { // react to the update }) unsubscribe()QueryObserver.subscribe-node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/infiniteQueryObserver.d.ts:25

Methods ​

bindMethods() ​

ts
protected bindMethods(): void;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/infiniteQueryObserver.d.ts:29

Returns ​

void

Overrides ​

QueryObserver.bindMethods


createResult() ​

ts
protected createResult(query, options): InfiniteQueryObserverResult<TData, TError>;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/infiniteQueryObserver.d.ts:82

Parameters ​

ParameterType
queryQuery<TQueryFnData, TError, InfiniteData<TQueryFnData, TPageParam>, TQueryKey>
optionsInfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>

Returns ​

InfiniteQueryObserverResult<TData, TError>

Overrides ​

QueryObserver.createResult


destroy() ​

ts
destroy(): void;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:52

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.destroy


fetchNextPage() ​

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

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/infiniteQueryObserver.d.ts:62

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 ​

ParameterType
options?FetchNextPageOptions

Returns ​

Promise<InfiniteQueryObserverResult<TData, TError>>

Example ​

ts
const { hasNextPage } = observer.getCurrentResult()

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

See ​

InfiniteQueryObserver#fetchPreviousPage


fetchOptimistic() ​

ts
fetchOptimistic(options): Promise<QueryObserverResult<TData, TError>>;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:133

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 ​

ParameterType
optionsQueryObserverOptions<TQueryFnData, TError, TData, InfiniteData<TQueryFnData, TPageParam>, TQueryKey>

Returns ​

Promise<QueryObserverResult<TData, TError>>

Example ​

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

Inherited from ​

QueryObserver.fetchOptimistic


fetchPreviousPage() ​

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

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/infiniteQueryObserver.d.ts:81

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 ​

ParameterType
options?FetchPreviousPageOptions

Returns ​

Promise<InfiniteQueryObserverResult<TData, TError>>

Example ​

ts
const { hasPreviousPage } = observer.getCurrentResult()

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

See ​

InfiniteQueryObserver#fetchNextPage


getCurrentQuery() ​

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

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:105

Returns the Query instance this observer is currently observing.

Returns ​

Query<TQueryFnData, TError, InfiniteData<TQueryFnData, TPageParam>, TQueryKey>

Inherited from ​

QueryObserver.getCurrentQuery


getOptimisticResult() ​

ts
getOptimisticResult(options): InfiniteQueryObserverResult<TData, TError>;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/infiniteQueryObserver.d.ts:43

The infinite-query counterpart of QueryObserver#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 ​

ParameterType
optionsDefaultedInfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>

Returns ​

InfiniteQueryObserverResult<TData, TError>

Overrides ​

QueryObserver.getOptimisticResult


hasListeners() ​

ts
hasListeners(): boolean;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/subscribable-CysQ0tne.d.ts:28

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

Returns ​

boolean

Inherited from ​

QueryObserver.hasListeners


onSubscribe() ​

ts
protected onSubscribe(): void;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:33

Returns ​

void

Inherited from ​

QueryObserver.onSubscribe


onUnsubscribe() ​

ts
protected onUnsubscribe(): void;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:34

Returns ​

void

Inherited from ​

QueryObserver.onUnsubscribe


refetch() ​

ts
refetch(__namedParameters?): Promise<QueryObserverResult<TData, TError>>;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:116

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

Parameters ​

ParameterType
__namedParameters?RefetchOptions

Returns ​

Promise<QueryObserverResult<TData, TError>>

Example ​

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

Inherited from ​

QueryObserver.refetch


setOptions() ​

ts
setOptions(options): void;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/infiniteQueryObserver.d.ts:36

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 ​

ParameterType
optionsInfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>

Returns ​

void

Overrides ​

QueryObserver.setOptions


shouldFetchOnReconnect() ​

ts
shouldFetchOnReconnect(): boolean;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:40

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

Returns ​

boolean

Inherited from ​

QueryObserver.shouldFetchOnReconnect


shouldFetchOnWindowFocus() ​

ts
shouldFetchOnWindowFocus(): boolean;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:46

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

Returns ​

boolean

Inherited from ​

QueryObserver.shouldFetchOnWindowFocus


trackProp() ​

ts
trackProp(key): void;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:101

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's proxy; exposed directly for adapters that track property access themselves (e.g. through their own reactivity system) instead of via the proxy.

Parameters ​

ParameterType
key| "data" | "error" | "status" | "isPending" | "isRefetching" | "refetch" | "isPaused" | "isError" | "isLoading" | "isLoadingError" | "isRefetchError" | "isSuccess" | "isPlaceholderData" | "dataUpdatedAt" | "errorUpdatedAt" | "failureCount" | "failureReason" | "errorUpdateCount" | "isFetched" | "isFetchedAfterMount" | "isFetching" | "isInitialLoading" | "isStale" | "isEnabled" | "fetchStatus"

Returns ​

void

Inherited from ​

QueryObserver.trackProp


trackResult() ​

ts
trackResult(result, onPropTracked?): QueryObserverResult<TData, TError>;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:94

Wraps a QueryObserverResult in a Proxy that records which properties are read, via QueryObserver#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 ​

ParameterType
resultQueryObserverResult<TData, TError>
onPropTracked?(key) => void

Returns ​

QueryObserverResult<TData, TError>

Inherited from ​

QueryObserver.trackResult


updateResult() ​

ts
updateResult(): void;

Defined in: node_modules/.bun/@tanstack+query-core@5.104.1/node_modules/@tanstack/query-core/build/modern/hydration-B9t5w6NC.d.ts:141

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.updateResult

Released under the MIT License.