Skip to content

react-query useInfiniteQuery and paginated queries #1

@grez72

Description

@grez72

Is it possible to use convexQuery with tanstack useInfiniteQuery and a paginated convex query?

I have infinite scroll working with the following react-query / convex integration, but it does not subscribe to changes when hooked up this way. (Note that I'm using react-query here so that I can leverage it's caching capabilities when navigating between nextjs routes, allowing me to "restore scroll position" on an infinite scroll page without having to refetch data).

const { data, fetchNextPage, hasNextPage, isLoading } = useInfiniteQuery({
    queryKey: ['entries'],
    queryFn: async ({ pageParam = 1 }) => {
      const numItems = 10;
      const currentCursor = pageParam === 1 ? null : data?.pages[data.pages.length - 1]?.continueCursor;
      return convex.query(api.feed.getUserFeed, {
        paginationOpts: { numItems: numItems, cursor: currentCursor },
      });
    },
    getNextPageParam: (lastPage, allPages) => {
      return (lastPage.isDone===true) ? undefined : allPages.length + 1;
    }
  });  

From the Demo in the Docs it wasn't clear to me whether you can just drop-in useInfiniteQuery here, and if so, how to handle the cursor logic.

example from docs:

import { useQuery } from "@tanstack/react-query";
import { convexQuery } from "@convex-dev/convex-react-query";
import { api } from "../convex/_generated/api";

export function App() {
  const { data, isPending, error } = useQuery(
    convexQuery(api.functions.myQuery, { id: 123 }),
  );
  return isPending ? "Loading..." : data;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions