Infinite query with app router initial data

Hi, I am having a hard times implementing ininite query with initial data passed from server components. Does any1 have an example of e.g procedure and implementation using app router and initial data? Thanks in advance
1 Reply
stanisław
stanisław5mo ago
Thats my client usage
const { data, fetchNextPage, hasNextPage, isFetchingNextPage } =
api.bestItems.getAll.useInfiniteQuery(
{
query: searchParams.get('query') ?? undefined,
style: searchParams.get('style') ?? undefined
},
{
initialData: {
pageParams: [initialData.cursor],
pages: [initialData]
},
getNextPageParam: (lastPage) => lastPage.cursor
}
);
const { data, fetchNextPage, hasNextPage, isFetchingNextPage } =
api.bestItems.getAll.useInfiniteQuery(
{
query: searchParams.get('query') ?? undefined,
style: searchParams.get('style') ?? undefined
},
{
initialData: {
pageParams: [initialData.cursor],
pages: [initialData]
},
getNextPageParam: (lastPage) => lastPage.cursor
}
);
and thats my trpc return
const items = await ctx.db.items.findMany({
cursor: input.cursor ? { id: input.cursor } : undefined,
take: take,
skip: input.cursor ? 1 : 0,
where
});

let nextCursor: typeof input.cursor;

if (items.length === take) {
const nextItem = items.at(-1);

nextCursor = nextItem?.id;
}

return {
items,
cursor: nextCursor
};
const items = await ctx.db.items.findMany({
cursor: input.cursor ? { id: input.cursor } : undefined,
take: take,
skip: input.cursor ? 1 : 0,
where
});

let nextCursor: typeof input.cursor;

if (items.length === take) {
const nextItem = items.at(-1);

nextCursor = nextItem?.id;
}

return {
items,
cursor: nextCursor
};
And initially I am passing thata from the server component like that
const initialData = await api.items.getAll.query({
query: searchParams?.query,
style: searchParams?.style
});

<ClientComponent initialData={initialData} />
const initialData = await api.items.getAll.query({
query: searchParams?.query,
style: searchParams?.style
});

<ClientComponent initialData={initialData} />
On the first render items are in an order X then after a short time they swap And we are not loading all the data so the cursors must be mismatched or so Idk
Want results from more Discord servers?
Add your server
More Posts
HTMX: script `hx-on::before-request` didn't finish before sending AJAX requestso I am playing with HTMX + Laravel. https://htmx.org/ https://laravel.com/docs/10.x I have this caHow can i correctly past the JS Object to the function in the template string correctly``` const payload = { test1: 1, test2: 2, }; const links = ` <a onCORS Errors with LocalHostI've been testing an application and up until yesterday everything was working fine. Now, I'm gettinSection tag wont cover the entire container height```html <main class="flex-1 bg-gray-800 h-full"> <section class="bg-gray-800 h-full"> How to do an auction systemI'm building a kind of store that an item can have a countdown and when it get to zero, it would be Monorepo UI package has no styling when importedI have a monorepo (turborepo) with a Next.js app and a UI package. In this UI package I have a ShadcCan two frontend services point to the same base url?for example we have a landing page: foobar.com which runs on service A. Once a user signs in, they atRPC react-query hook not called on loadHi there, my tRPC hook is not being called on page load and I can't figure out why. Here's what my Next-auth.js: Redirect to custom URL after successful login.Hey all, I'm trying to figure out how to redirect after a successful login to a custom url. SpecificI has a long-running server task. I want to update my db afterwards. It's on the edge.Should I use TRPC on the edge runtime or prisma or update by supabase db direct? what's the cleanest