Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react-query useInfiniteQuery and paginated queries #1

Open
grez72 opened this issue Jul 22, 2024 · 2 comments
Open

react-query useInfiniteQuery and paginated queries #1

grez72 opened this issue Jul 22, 2024 · 2 comments

Comments

@grez72
Copy link

grez72 commented Jul 22, 2024

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;
}
@messabih-khalil
Copy link

did u found any solution

@thomasballinger
Copy link
Collaborator

The suggested approach is to use useConvexPaginatedQuery,, a re-exported version of usePaginatedQuery from the convex package, for reactive pagination.

Pagination works a bit differently in TanStack Query and

The direction I'd like to take is to improve convex's usePaginatedQuery hook to stay subscribed / keep the paginated query around, but that's a significant change because currently the state for the paginated query is stored in the React hook.

Making useInfiniteQuery work is also an option but the interfaces dont' mesh naturally, if it is supported it will likely look like what you've done here, something non-reactive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants