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
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 queryqueryFn- The function that the query will use to fetch data. Receives apageParamin the contextinitialPageParam- The default page parameter to use for the first pagegetNextPageParam- Function to get the next page parameter from the last page’s datagetPreviousPageParam- Function to get the previous page parameter from the first page’s dataenabled- Whether the query should run automaticallystaleTime- Time in milliseconds before data is considered stalegcTime- Time in milliseconds before inactive queries are garbage collected- And other standard query options
Additional configuration for the infinite query injection.
Returns
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 pagespageParams- Array of all page parameters
error- The error object if the query failedstatus- The status of the query (pending, error, success)isPending- Boolean indicating if the query is in pending stateisError- Boolean indicating if the query failedisSuccess- Boolean indicating if the query succeededhasNextPage- Boolean indicating if there is a next page to fetchhasPreviousPage- Boolean indicating if there is a previous page to fetchisFetchingNextPage- Boolean indicating if the next page is currently being fetchedisFetchingPreviousPage- Boolean indicating if the previous page is currently being fetched
fetchNextPage()- Function to fetch the next pagefetchPreviousPage()- Function to fetch the previous pagerefetch()- 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
initialData is provided and defined, the query result is guaranteed to have data available immediately.
With undefined initial data
initialData is not provided or is undefined, the query result data may be undefined initially.
Implementation Details
- Uses
InfiniteQueryObserverinternally 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
injectoroption is provided - The
queryFnreceives apageParamproperty in its context object - You must provide
initialPageParam,getNextPageParam, and optionallygetPreviousPageParam - The
dataobject has a specific structure withpagesandpageParamsarrays - Each call to
fetchNextPageorfetchPreviousPageappends to the pages array - The query is automatically managed and cleaned up when the component/service is destroyed