Infinite rerender loop #8759
Answered
by
MathisEngels
MathisEngels
asked this question in
Q&A
-
Hi everyone, I'm working on a Next.js project, using TanStack Query with SSR and Streaming my queries. The code: /** page.tsx **/
export default async function Profile() {
const queryClient = getQueryClient(); <- From 'Advanced Server Rendering' docs.
...
// To handle API failure
try {
await queryClient.fetchQuery({
queryKey: ["profile"],
queryFn: () => getProfile(someUUID),
});
} catch {
...
}
return (
<HydrationBoundary state={dehydrate(queryClient)}>
<div>
...
<WishlistPanel />
</div>
</HydrationBoundary>
);
} /** WishlistPanel.tsx **/
"use client";
export default function WishlistPanel() {
const [itemQuery, profileQuery] = useSuspenseQueries({
queries: [
...
{ queryKey: ["profile"], queryFn: () => getProfile(someUUID) },
],
});
...
return (
<div>
<WishlistCTA />
...
{Display some profileQuery.data}
</div>
);
} /** WishlistCTA.tsx **/
"use client";
export default function WishlistCTA() {
console.log("rendering cta");
const queryClient = getQueryClient();
const { data: profile } = useSuspenseQuery({
queryKey: ["profile"],
queryFn: () => getProfile(someUUID),
});
const { mutate: togglePrivacy, isPending } = useMutation({
mutationFn: () => {
return patchWishlist(someUUID, !profile.wishlist.private);
},
onSuccess: async (oldData) => {
await queryClient.invalidateQueries({ queryKey: ["profile"] });
},
});
return (
<div>
<Toggle onClick={() => togglePrivacy()} disabled={isPending}>
{profile.wishlist.private}
</Toggle>
...
</div>
);
} For some reason, when ever I trigger 'togglePrivacy' once, Do you have any idea why? Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Answered by
MathisEngels
Mar 6, 2025
Replies: 1 comment 1 reply
-
In client components, you need to get the queryClient with |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @TkDodo, thanks for answering:
After a lot of debugging, I found what triggered the infinite re-render loop.
In
WishlistPanel.tsx
, I had a TanStack Table displaying profile related data.Before displaying this data, I was doing something like: