Skip to Content
DocsReact HooksInfinite Queries

Infinite Queries

For lists that load more data as the user scrolls or clicks a “Load More” button, Actyx RPC provides the useInfiniteQuery hook. It handles paginated data loading forward, tracks multiple pages in a single cache entry, flattens the records for simple rendering, and supports optimistic cache mutations.


useInfiniteQuery

To fetch forward-only paginated data:

import { useInfiniteQuery } from "@explita/actyx-rpc/react"; import { getFeedPosts } from "@/backend/procedures"; function Feed() { const { data: posts, // Flattened TPage[] array across all loaded pages pages, // Raw array of page objects: InfiniteQueryPage<TPage>[] fetchNext, hasNext, isFetching, isRefetching, isEmpty, error, refetch, } = useInfiniteQuery( getFeedPosts, { initialInput: { limit: 10 }, initialPageParam: undefined, // Start at the beginning getNextPageParam: (lastPage) => lastPage.nextCursor, queryKey: ["feed"], } ); if (isFetching && !isRefetching) return <p>Loading feed...</p>; if (error) return <p>Error loading feed: {error.message}</p>; if (isEmpty) return <p>No posts available.</p>; return ( <div> <ul> {posts.map((post) => ( <li key={post.id}> <h3>{post.title}</h3> <p>{post.content}</p> </li> ))} </ul> {hasNext && ( <button onClick={() => fetchNext()} disabled={isFetching} > {isFetching ? "Loading more..." : "Load More"} </button> )} {isRefetching && <p>Updating background feed...</p>} <button onClick={() => refetch()}>Refresh Feed</button> </div> ); }

Configuration Options

The useInfiniteQuery hook accepts two arguments:

  1. proc: A procedure function that handles the server query.
  2. options: An optional configuration object (UseInfiniteQueryOpts):
OptionTypeDefaultDescription
initialInputWithoutCursor<TInput>Base input parameters for the procedure (excluding the cursor).
initialPageParamstring | numberundefinedThe cursor parameter to use for the first page fetch.
getNextPageParam(lastPage, allPages) => cursorCallback to determine the cursor parameter for the next page. If not specified, defaults to checking lastPage.nextCursor.
queryKeyunknown[]Unique array key for caching and invalidation.
maxPagesnumberMaximum number of pages to keep in the cache. Discards oldest pages when exceeded.
enabledbooleantrueSet to false to disable automatic loading on mount.
staleTimenumber | string0Time in milliseconds (or string window e.g., "5m") before data is considered stale.
gcTimenumber | string5 * 60 * 1000Time in milliseconds (or string window) before unused cache data is garbage collected.
refetchIntervalnumber0Time in milliseconds for automatic background polling.
refetchOnWindowFocusbooleanfalseAutomatically refetch when the window regains focus.
refetchOnReconnectboolean | "always"trueRefetch when internet connection is restored.
initialDatadata | (() => data)Pre-populates the cache pages on mount.
onSuccess(data) => voidCallback run on successful page load.
onError(error) => voidCallback run when a fetch fails.
onSettled() => voidCallback run when a query completes (success or failure).

Returned Properties

PropertyTypeDescription
dataTPage[]Flattened array containing the combined items from all fetched pages.
pagesTFullPage[]The raw array of fetched page structures.
pageParams(string | number)[]The array of cursors/page parameters fetched so far.
hasNextbooleanTrue if another page is available to fetch.
fetchNext() => Promise<TFullPage | undefined>Triggers fetching the next page of data.
isFetchingbooleanIndicates if a fetch request is currently in progress.
isRefetchingbooleanIndicates if a background refresh of the existing pages is in progress.
isSuccessbooleanTrue if the last fetch was successful.
isErrorbooleanTrue if the last fetch failed.
errorErrorResponse | undefinedThe error object if the fetch failed.
isFetchedbooleanTrue if the query has successfully resolved at least once.
isEmptybooleanTrue if the query resolved successfully and the flattened data array is empty.
refetch() => Promise<void>Clears cursors and refetches starting from the first page.
reset() => voidResets the hook state to initial settings.

Cache Mutation Helpers

useInfiniteQuery also returns helper methods for modifying local cache state optimistically without causing a network refetch. All of these methods return a rollback() function.

  • remove(index | predicate): Removes an item from the cache.
  • update(index | predicate, updater): Updates an item in the cache.
  • prepend(item): Inserts a new item at the beginning of the first page.
  • append(item): Inserts a new item at the end of the last page.
  • insert(index, item): Inserts an item at a specific flattened index.
  • setPages(updater): Direct access to mutate the pages array.
  • snapshot(): Takes a snapshot of current cache state to restore later.

For full examples of cache mutations and rollbacks, see the Cache Mutations & Rollbacks documentation page.

Last updated on