Maximum update depth exceeded #8677
Send to my agentDescribe the bug#
I am getting the error "Maximum update depth exceeded"
Your minimal, reproducible example#
https://stackblitz.com/~/github.com/yWilliamLima/test-error
Steps to reproduce#
It usually occurs after a while of running whenever changing pages/routes
Expected behavior#
Do not return this error
How often does this bug happen?#
Sometimes
Screenshots or Videos#
Platform#
OS: Windows 11 Browser: Chrome Node: 22.12.0
Tanstack Query adapter#
None
TanStack Query version#
5.66.5
TypeScript version#
5.7.3
Additional context#
The error in the console does not occur locally, only in stackblitz Maybe because of this error in the stackblitz console it doesn't happen, because it gives a refresh Because it is a minimal example, it takes a while to occur, but in my project it occurs frequently
I started getting this error after upgrading as well. I narrowed it down to version v5.66.4 introducing this behavior.
Your test is very good, the problem was actually introduced in v5.66.4, I downgraded to v5.66.3 and the problem does not occur
@juliusmarminge 5.66.4 was our hydration changes 😕
I think the following part of the logic added by #8383 in the PR might be causing some problems.
This is because the code in the above link thinks that the new logic is new if the “promise.status” is different.
But https://github.com/TanStack/query/pull/8383/files#diff-da66ebb93eacffd07c6ea6e697db57d6091050af95da336ea10e1aaedb07ef7cR86 is chasing a “promise”, so I think a new “promise” will be created each time. (or is the “promise.status” actually different each time?)
Wouldn't that cause “Maximum update depth exceeded” to be thrown because the new code thinks it's new every time?
can anyone provide a minimal repro? I can look into this tonight but would help to have something simple to debug
I did some validations in my personal project where the error occurs frequently, where if I export the Page as async and use await in prefetchQuery the problem does not occur:
export default async function Page() { const queryClient = getQueryClient() await queryClient.prefetchQuery({ ...getApiV1ActivitiesOptions() })yea but then you're no longer sending promises down so you're hitting completely different code paths with that - not really comparable
I just want a state where it's failing on that PR I linked 😅
@juliusmarminge
A simple way to reproduce it is to add an artificial delay to the “getApiV1ActivitiesOptions” function in your temporary repo PR(#8682 ) in the file “integrations/react-next-15/app/query.ts” (
await new Promise((r) => setTimeout(r, 5000))) If you comment and check back later when you're patching the delay, you should see an infinite render.A simple way to reproduce it is to add an artificial delay to the “getApiV1ActivitiesOptions” function in your temporary repo PR(#8682 ) in the file “integrations/react-next-15/app/query.ts” (
await new Promise((r) => setTimeout(r, 5000))) If you comment and check back later when you're patching the delay, you should see an infinite render.(I used a translator) I don't know if this is a bug or intended behavior.
In React Query versions up to 5.66.3, only the dataUpdatedAt field was compared during hydration, which prevented this issue from occurring. However, starting with version 5.66.4, the dehydrated state now also includes pending queries because the shouldDehydrateQuery setting returns true even when query.state.status is 'pending'. This means that queries that have not yet completed are being included in the dehydrated state.
During hydration, React Query internally attaches .then() and .catch() chains to these pending queries, effectively generating new Promise objects—especially when error redaction logic is applied. As a result, each render creates a new Promise instance, and the promise.status comparison logic interprets this as the query being “new” or updated. This triggers re-hydration, which in turn causes another render, leading to an infinite render loop.
The problem is exacerbated in a development environment, particularly with Next.js' Hot Module Replacement (HMR) or Fast Refresh. When code changes while a query is still pending, the old pending Promise conflicts with the new code. This clash makes the infinite rendering issue much more likely to occur. In production builds, where a full page reload happens, this issue tends not to manifest as prominently.
To reproduce the issue, use React Query version 5.66.4 or later with a configuration that includes pending queries in the dehydrated state (i.e., shouldDehydrateQuery returns true when query.state.status is 'pending'). Then, introduce an artificial delay in the getApiV1ActivitiesOptions function—for example, by adding
await new Promise(r => setTimeout(r, 5000))This delay causes the query to remain pending. Next, while the query is still pending, change the code via HMR—for instance, by uncommenting and then commenting out the delay code. At this point, because the server-side (or RSC) prefetchQuery has run dehydrate while the query was still pending, the client’s HydrationBoundary receives a dehydrated state that includes the old pending Promise. The new code then interprets the Promise as “new” on every render, triggering an infinite render loop.
I don't know if this is the intended behavior of “@tanstack/react-query”, but if you want to reproduce the above issue, you can do it like this (It is quite an edge case and will probably only be reproduced in HMR development environments.)
oh you're changing the queryFn... that's annoying to test xd
Yeah, the idea behind the reproduction was to intentionally reproduce the “promise” changing while “pending”. I don't know if this will happen in the real world,,
@juliusmarminge This is happening with failed queries for me. What I think is happening is that:
- The
HydrationBoundarycode is expected to be idempotent, if it rerenders with the same state it shouldn't hydrate anything - The change introduced this:
dehydratedQuery.promise.status !== existingQuery.promise.status, but sincepromise.statuscan change from pending to failed without thestatechanging, we end up in an infinite loop where the query keeps being re-added to thehydrationQueue, changing the status, triggering being re-added etc.
- The
Ephem(GitHub profile, opens in a new tab) closed this
Ephem(GitHub profile, opens in a new tab) reopened this
Closing was accidental.
Encountered this issue today with minimal tRPC setup, using prefetch and HydrateClient in a Next.js
layout.tsxfile.I can also confirm that downgrading RQ to
5.66.3resolves the issue for good.Very hard to reproduce, seemingly random, then consistent, then random again, but almost always when something new is added to the code and hot reload is triggered, but if I refresh, it never appears again.
I am working on creating a minimal repro, but wanted to share my experience so far.
I used this exact setup a month ago without issues.
I'm having the same experience as Antonio.
It started happening after upgrading tanstack query and following this PR https://github.com/t3-oss/create-t3-turbo/pull/1305 from create-t3-turbo to migrate to the new trpc queryOptions api.
I can't consistenly get the error to appear, but it seems to happen most often for me on page navigation. Occasionally hot reloading seems to cause it as well.
I think @Ephem already did a root cause analysis on this bug. @juliusmarminge @Ephem can you think about a fix for this ?
TkDodo added labelsbug
TkDodo added labelspackage: react-query
@TkDodo Yeah, I haven't really verified the details a 100%, but I'm pretty sure it's along those lines. I'm not sure if there's a quick fix, I haven't sat down to think about it. Even if there is one I still believe that hydrating every query that has a different status might not be safe for other reasons, see original comment in the PR.
+1
@AntonioErdeljac
The error occurs when I try get the data from the new pages GetAllProducts(page=1, size=10). Before it, the RQ works well.
I tried downgrading the RQ version. But it didn't work. I used fetch data to put it in a Shadcn table. I guess RQ and Shadcn aren't a good match. I don't know why yet, but the error is in the Shadcn table
The logger said that the error occurs in this line:
{table.getRowModel().rows?.length ? (if i find out something I back here.
@Merus23 What you're seeing is probably an unrelated error. Look into how your table component's parent is rendered, how table is getting data and whether the data is referentially stable.
I am experiencing this error with RQ version 5.66.9 and tRPC 11 after throwing error in TRPC procedure ( given that the procedure is being prefetched in server component ) I am following the basic setup described here: https://trpc.io/docs/client/tanstack-react-query/server-components#leveraging-suspense
I don't know much about under the hood, but I get this seemingly if I have a prefetched query that never gets consumed.
In the below example, it works totally fine, however if I comment out
<PokemonInfo />then I start getting the exceeded error.export default function Home() { const queryClient = getQueryClient(); void queryClient.prefetchQuery(pokemonOptions); return ( <HydrationBoundary state={dehydrate(queryClient)}> <PokemonInfo /> </HydrationBoundary> ); }I have a fix up in #9157, if you want to help verify it works for you, you can install the prerelease packages with:
npm i https://pkg.pr.new/@tanstack/query-core@1568117 npm i https://pkg.pr.new/@tanstack/react-query@1568117(You can verify those are the packages from the PR and find any new ones in case the PR updates by going to the Checks and clicking
pkg.pr.new)TkDodo closed this
I thought my PR would solve this, and I'm pretty sure it solves some versions of it, but I've seen this pop up again even after the PR, so reopening to investigate.
Ephem(GitHub profile, opens in a new tab) reopened this
Ephem(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
I started getting this error after upgrading as well. I narrowed it down to version v5.66.4 introducing this behavior.