Skip to content

Bug: "Maximum update depth exceeded" when unrelated updates arrive in separate tasks #38428

Backlog

Description

@phibr0

Update: See #38428 (comment)

React version: 19.3.1 (works fine in 18.3.1)

Steps To Reproduce

  1. Render 30 rows like the ones below.
  2. Load each row's data once, each in its own setTimeout(0). This is what TanStack Query does when it notifies queries.
function Placeholder() {
  const [visible, setVisible] = useState(true);
  useEffect(() => {
    setVisible(true); // already true
  }, []);
  return visible ? <span>…</span> : null;
}

function Content({ store, onStatusChange }) {
  const loaded = useSyncExternalStore(store.subscribe, store.getSnapshot);
  useLayoutEffect(() => {
    if (loaded) onStatusChange('loaded');
  }, [loaded, onStatusChange]);
  return loaded ? <b>content</b> : null;
}

function Row({ store }) {
  const [, setStatus] = useState('loading');
  return (
    <div>
      <Content store={store} onStatusChange={setStatus} />
      <Placeholder />
    </div>
  );
}

stores.forEach((store) => setTimeout(() => store.load(), 0));

Repro: https://gist.github.com/phibr0/5c6f2fd5b053eebc93d398bee3b4e3bf

We hit this in a table with a Base UI avatar and one query per row.

The current behavior

React throws "Maximum update depth exceeded", but nothing is actually looping. Each row's store changes once, which means two commits per row. With 30 rows that's 60 commits, which is just over the limit of 50.

It goes away if I:

  • switch to React 18.3.1,
  • remove the setVisible(true), or
  • load all rows in the same task.

The expected behavior

No error, same as in React 18.

Notes

Here's what claude thinks is going on:

  • setVisible(true) bails out, but the update stays in the queue.
  • When the layout effect triggers a sync re-render, that leftover update gets skipped, and DefaultLane stays pending on the root (ReactFiberHooks.js#L1429).
  • The nested update check counts a commit whenever any SyncUpdateLanes are still pending, even ones that commit didn't schedule (ReactFiberWorkLoop.js#L4284-L4293). The next row's timer fires before the leftover work gets a chance to render, so the count keeps climbing from row to row.
  • React 18 only counted a pending SyncLane, which would explain why 18.3.1 is fine. Maybe the check could only count lanes that were scheduled during that commit?

Related reports

Other libraries seem to be running into the same thing:

  • Base UI's Autocomplete throws when you type fast, from a same-value setState in an effect (mui/base-ui#5994).
  • Material UI's TextField and Popper throw for the same reason, and also work fine on 18.3.1 (mui/material-ui#49701). They've worked around it on their side (#49703).
  • react-admin throws on list pages with one react-query query per row, with a stack going through useSyncExternalStore (marmelab/react-admin#11392).

Activity

  1. added
    Status: Unconfirmed
    on Nov 28, 2026
  2. changed the title [-]Bug: "Maximum update depth exceeded" without an update loop, after a same-value setState[/-] [+]Bug: "Maximum update depth exceeded" when unrelated updates arrive in separate tasks[/+] on Nov 28, 2026
  3. phibr0 commented on Nov 28, 2026

    @phibr0
    Author

    I looked into this further and the title was too narrow: the same-value setState isn't required. I've renamed the issue.

    Simpler repro

    No same-value update, no layout effect. 60 rows, each store loaded in its own setTimeout(0):

    function Row({ store }) {
      const loaded = useSyncExternalStore(store.subscribe, store.getSnapshot);
      const [status, setStatus] = useState('loading');
      useEffect(() => {
        if (loaded) setStatus('loaded');
      }, [loaded]);
      return <div>{status}</div>;
    }
    
    stores.forEach((store) => setTimeout(() => store.load(), 0));

    This throws "Maximum update depth exceeded" on the 53rd row. Each row updates exactly once.

    What is actually going on

    The nested update counter goes up after every commit that leaves SyncLane, InputContinuousLane or DefaultLane work pending, and it only goes back to zero after a commit that leaves none:

    // Check if this render scheduled a cascading synchronous update. This is a
    // heurstic to detect infinite update loops. We are intentionally excluding
    // hydration lanes in this check, because render triggered by selective
    // hydration is conceptually not an update.
    if (
    // Was the finished render the result of an update (not hydration)?
    includesSomeLane(lanes, UpdateLanes) &&
    // Did it schedule a sync update?
    includesSomeLane(remainingLanes, SyncUpdateLanes)

    • Each store update renders synchronously and commits.
    • That commit leaves DefaultLane work pending. In the repro above it is the update from the passive effect; in my original repro it was the skipped same-value update.
    • The Scheduler task for that work doesn't get to run before the next timer fires, so there is never a commit that leaves nothing pending.

    So commits from unrelated updates add up as if they were one cascade. Nothing resets the counter between independent tasks.

    Corrections to my original notes

    • "Maybe the check could only count lanes that were scheduled during that commit" fixes my original repro, but not the one above, where the commit really does schedule the pending work.
    • React 18.3.1 only counted a pending SyncLane. The check was widened to SyncLane | InputContinuousLane | DefaultLane in Bugfix: Selective hydration triggers false update loop error  #27439.

    I have a fix with tests and will open a PR. (#38429)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: Unconfirmed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions