Skip to content

How to do infinite query with route loader? #3855

Answered by AhmedBaset
lukedukeus asked this question in Q&A
Discussion options

You must be logged in to vote

Use infiniteQueryOptions to share the query logic between useSuspenseInfiniteQuery (in the component) and queryClient.prefetchInfiniteQuery (in the loader):

import { infiniteQueryOptions, useSuspenseInfiniteQuery } from "@tanstack/react-query";
import { createFileRoute } from "@tanstack/react-router";

const options = infiniteQueryOptions({
  queryKey: ["posts"],
  initialPageParam: 0,
  getNextPageParam: (lastPage) => lastPage.nextPage,
  queryFn: fetchPosts,
});

export const Route = createFileRoute("/dashboard/")({
  loader: ({ context }) => {
    await context.queryClient.prefetchInfiniteQuery(options);
  },
  component: RouteComponent,
});


function RouteComponent() {
  const query = 

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@lukedukeus
Comment options

@lukedukeus
Comment options

Answer selected by lukedukeus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants