[{"data":1,"prerenderedAt":6},["ShallowReactive",2],{"post-content-react-maximum-update-depth-exceeded-loop":3},{"content":4,"lastModified":5},"\u003Cp>The dashboard had been eating a CPU core for eleven days before anyone noticed. It was an internal page only I opened, and the fan announced the problem before I checked anything else. Nine hundred requests to \u003Ccode>\u002Fapi\u002Fmetrics\u003C\u002Fcode> in about a minute, the network panel scrolling like a slot machine. Sentry was empty and the console stayed quiet.\u003C\u002Fp>\n\n\u003Cp>If you have ever gone looking for a fix for \"Maximum update depth exceeded\", you know the message. What took me a while to accept is that the error is a courtesy, not a guarantee. React only reports the loops it manages to catch, and mine was one it never caught.\u003C\u002Fp>\n\n\u003Cimg src=\"https:\u002F\u002Fimages.pexels.com\u002Fphotos\u002F39625372\u002Fpexels-photo-39625372.jpeg?auto=compress&cs=tinysrgb&w=1600\" alt=\"Black metal spiral staircase against a sunlit wall\" loading=\"lazy\" \u002F>\n\n\u003Ch2>What the limit actually counts\u003C\u002Fh2>\n\n\u003Cp>Eventually I opened the react-dom 19.3.1 source and read it. Three separate limits live in there, and they are not the same thing.\u003C\u002Fp>\n\n\u003Cp>\u003Ccode>NESTED_UPDATE_LIMIT\u003C\u002Fcode> is 50. When a commit schedules another update on the same root, React bumps a counter, and once that counter passes 50 it throws. The wording of that throw mentions \u003Ccode>componentWillUpdate\u003C\u002Fcode> and \u003Ccode>componentDidUpdate\u003C\u002Fcode>, which tells you how old that branch is.\u003C\u002Fp>\n\n\u003Cp>\u003Ccode>NESTED_PASSIVE_UPDATE_LIMIT\u003C\u002Fcode> is also 50, and it guards updates scheduled from \u003Ccode>useEffect\u003C\u002Fcode>. This is the part most write-ups skip. When that counter trips, React resets it and calls \u003Ccode>console.error\u003C\u002Fcode>. It does not throw. In the code I read, an effect-driven loop is a log line, and you get roughly one of them per fifty wasted cycles.\u003C\u002Fp>\n\n\u003Cp>The third limit, \u003Ccode>RE_RENDER_LIMIT\u003C\u002Fcode>, is 25. It covers something else entirely: \u003Ccode>setState\u003C\u002Fcode> called during render, where React re-runs the same component body in place. Different counter, different message: the one about too many re-renders. Several posts I read tangled all three together, which is how people end up adding \u003Ccode>useMemo\u003C\u002Fcode> to a component that \u003Ca href=\"\u002Fposts\u002Freact-context-re-render-why-memo-doesnt-help\u002F\">never had an identity problem\u003C\u002Fa>.\u003C\u002Fp>\n\n\u003Ch2>Why my loop stayed silent\u003C\u002Fh2>\n\n\u003Cp>The counter is not a render count. It counts consecutive commits where the previous commit scheduled a new update. A commit that schedules nothing resets it to zero.\u003C\u002Fp>\n\n\u003Cp>That one sentence explains the whole eleven days. My effect awaited a fetch. The state update landed in a later task, after React had committed and moved on, so the counter never accumulated. The loop was real, it just kept breaking its own chain before anyone could count it. Eleven days at fifteen requests a second is a billing conversation I would rather not have had.\u003C\u002Fp>\n\n\u003Cp>Here is the code that did it. \u003Ccode>range\u003C\u002Fcode> is a fresh object on every render, so the dependency never compares equal:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-jsx\">function SpendChart({ accountId, from, to }) {\n  const [rows, setRows] = useState([]);\n  const range = { from, to }; \u002F\u002F new object, every render\n\n  useEffect(() => {\n    fetchMetrics(accountId, range).then(setRows);\n  }, [accountId, range]); \u002F\u002F never stable\n\n  return &lt;Chart data={rows} \u002F&gt;;\n}\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>Then came the refactor that finally produced an error. I added a clamp step that wrote the range back into state synchronously, before the fetch. No \u003Ccode>await\u003C\u002Fcode>, no gap, so the update landed inside the commit cycle and the counter climbed. The page threw on mount and I have never been so relieved to see a stack trace.\u003C\u002Fp>\n\n\u003Cp>That is the trap. The async version of a loop hides itself. Take the \u003Ccode>await\u003C\u002Fcode> out and the same bug becomes loud.\u003C\u002Fp>\n\n\u003Ch2>The fix is usually subtraction\u003C\u002Fh2>\n\n\u003Cp>Almost every loop I have written came from copying a value into state to use it elsewhere. Build it where it is used instead, and depend on primitives:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-jsx\">function SpendChart({ accountId, from, to }) {\n  const [rows, setRows] = useState([]);\n\n  useEffect(() =&gt; {\n    let cancelled = false;\n    fetchMetrics(accountId, { from, to }).then((next) =&gt; {\n      if (!cancelled) setRows(next);\n    });\n    return () =&gt; {\n      cancelled = true;\n    };\n  }, [accountId, from, to]); \u002F\u002F primitives only\n\n  return &lt;Chart data={rows} \u002F&gt;;\n}\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>The object now gets built inside the effect, where nothing watches its identity. Three primitives replace one object that churned on every render, and the effect runs when the inputs actually change.\u003C\u002Fp>\n\n\u003Ch2>When the loop is legitimate\u003C\u002Fh2>\n\n\u003Cp>Some loops are genuine synchronization, where a refetch really should replace the rows. A polling timer will do it, and so will two components writing the same state. For those, bail out of the render instead of trying to prevent the update:\u003C\u002Fp>\n\n\u003Cpre>\u003Ccode class=\"language-jsx\">\u002F\u002F next is the rows you just fetched\nsetRows((prev) =&gt; (sameIds(prev, next) ? prev : next));\u003C\u002Fcode>\u003C\u002Fpre>\n\n\u003Cp>Returning the previous reference makes React skip the re-render. The effect still fires and the request still goes out, but the browser stops painting work nobody can see. It buys you time to find the real source.\u003C\u002Fp>\n\n\u003Cp>Two more shapes worth knowing fail with different text. A \u003Ccode>useSyncExternalStore\u003C\u002Fcode> subscription whose \u003Ccode>getSnapshot\u003C\u002Fcode> returns a new object logs \"The result of getSnapshot should be cached to avoid an infinite loop\". A measuring ref callback that sets state will loop too, because an inline arrow ref is a new function on every render and React detaches and reattaches it. Same disease, different symptom.\u003C\u002Fp>\n\n\u003Ch2>How to catch one in under a minute\u003C\u002Fh2>\n\n\u003Cp>Do not wait for the error. Open the network panel first, sorted by count. A page that should fetch once per interaction but shows forty identical requests is looping, whether or not React says anything. I run that check before reading code now. It takes seconds and never lies.\u003C\u002Fp>\n\n\u003Cp>If the requests look clean, drop \u003Ccode>console.count\u003C\u002Fcode> into the component body and watch it climb while the page sits idle. \u003Ca href=\"\u002Fposts\u002Freact-useeffect-runs-twice-strict-mode\u002F\">Strict Mode will double a loop\u003C\u002Fa>, which is why I now stop the dev server, run a production build, and reproduce there before I trust a number. And when the error does appear, read the component stack before reaching for \u003Ccode>useMemo\u003C\u002Fcode>. The loop is nearly always in the effect that owns the setter, not in the component that re-rendered.\u003C\u002Fp>\n\n\u003Cp>My corrected version of that effect and the one-line bailout guard now live in my snippet library, mostly so I stop retyping them at 1am. That is what \u003Ca href=\"\u002Fsnippetark\u002F\">Snippet Ark\u003C\u002Fa> is for. The fan is quiet again, which is the only metric I actually trust.\u003C\u002Fp>\n","2026-11-14",1794910419993]