Skip to main content

injectInfiniteQuery

Injects an infinite query: a declarative dependency on an asynchronous source of data that is tied to a unique key. Infinite queries can additively “load more” data onto an existing set of data or “infinite scroll”.

Signature

Parameters

() => CreateInfiniteQueryOptions
required
A function that returns infinite query options. The function is run in a reactive context, similar to Angular’s computed. This allows signals to be inserted into the options, making the query reactive.CreateInfiniteQueryOptions properties:
  • queryKey - A unique key for the query
  • queryFn - The function that the query will use to fetch data. Receives a pageParam in the context
  • initialPageParam - The default page parameter to use for the first page
  • getNextPageParam - Function to get the next page parameter from the last page’s data
  • getPreviousPageParam - Function to get the previous page parameter from the first page’s data
  • enabled - Whether the query should run automatically
  • staleTime - Time in milliseconds before data is considered stale
  • gcTime - Time in milliseconds before inactive queries are garbage collected
  • And other standard query options
InjectInfiniteQueryOptions
Additional configuration for the infinite query injection.

Returns

object
A signal-based infinite query result object with the following properties:Properties:
  • data - Object containing all pages and page parameters:
    • pages - Array of all fetched pages
    • pageParams - Array of all page parameters
  • error - The error object if the query failed
  • status - The status of the query (pending, error, success)
  • isPending - Boolean indicating if the query is in pending state
  • isError - Boolean indicating if the query failed
  • isSuccess - Boolean indicating if the query succeeded
  • hasNextPage - Boolean indicating if there is a next page to fetch
  • hasPreviousPage - Boolean indicating if there is a previous page to fetch
  • isFetchingNextPage - Boolean indicating if the next page is currently being fetched
  • isFetchingPreviousPage - Boolean indicating if the previous page is currently being fetched
Methods:
  • fetchNextPage() - Function to fetch the next page
  • fetchPreviousPage() - Function to fetch the previous page
  • refetch() - Function to manually refetch all pages
  • And other standard query result properties

Usage

Basic example

Reactive example with signals

Bi-directional infinite scroll

Accessing all pages data

Using with custom injector

Offset-based pagination

Type Overloads

With defined initial data

When initialData is provided and defined, the query result is guaranteed to have data available immediately.

With undefined initial data

When initialData is not provided or is undefined, the query result data may be undefined initially.

Implementation Details

  • Uses InfiniteQueryObserver internally to manage infinite query state
  • Runs in the provided or current injection context
  • The query function is reactive and will re-run when any signals used inside it change
  • The query is automatically managed and cleaned up when the component/service is destroyed

Notes

  • Must be called within an injection context (constructor, field initializer, or factory function) unless the injector option is provided
  • The queryFn receives a pageParam property in its context object
  • You must provide initialPageParam, getNextPageParam, and optionally getPreviousPageParam
  • The data object has a specific structure with pages and pageParams arrays
  • Each call to fetchNextPage or fetchPreviousPage appends to the pages array
  • The query is automatically managed and cleaned up when the component/service is destroyed

See Also