Skip to content
Source
Sign inSign up
TanStack/redact

Infinite update loop when a cached child element's effect sets parent state (0.2.3) #120

Send to my agent
Openbryantgillespie(GitHub profile, opens in a new tab) opened this issue 1 comment

Summary

When a parent re-renders and returns the same cached element for a child, React skips that child. Redact 0.2.3 renders it again. If the child has an effect that sets parent state, the result is an infinite loop that ends in flushPending exceeded 50 iterations — suspected infinite update loop.

This looks like the same mechanism described in this comment on #75, where it shows up as extra render cost. Here it is a correctness failure: the same code runs once in React and never settles in Redact.

Reproduction

No compiler, StrictMode, router or hydration involved.

import { useState, useMemo, useLayoutEffect } from 'react'
import { createRoot } from 'react-dom/client'

let childRenders = 0

function Child({ onSnapshot }) {
  childRenders++
  useLayoutEffect(() => {
    onSnapshot({ at: childRenders })
  })
  return null
}

function Parent() {
  const [, setSnapshot] = useState(null)
  const onSnapshot = useMemo(() => (s) => setSnapshot(s), [])
  const child = useMemo(() => <Child onSnapshot={onSnapshot} />, [onSnapshot])
  return child
}

createRoot(document.getElementById('root')).render(<Parent />)
RuntimeResult
React 19.3.1Child renders once, no error
Redact 0.2.3Child renders on every parent render, throws flushPending exceeded 50 iterations

Both runs used jsdom and NODE_ENV=development, importing Redact's built dist/react/index.js and dist/dom/client.js directly.

Real-world impact

We hit this in a TanStack Start app (React Compiler on, Cloudflare Workers). A connection component publishes its session upward from a layout effect, and the provider stores it in state. The provider memoizes the connection element so the publish cannot re-render the publisher, which is a guarantee React gives. Under Redact the signed-in app froze as soon as the realtime connection came up. With the plugin removed, the same steps produced no errors.

The rest of the app worked well under Redact: lists, the editor, dialogs, mutations, and command palette. The entry chunk dropped from 128.0 KB to 67.6 KB gzip.

The React Compiler caches JSX in almost every component, so this pattern (skip the child when the element is unchanged) is probably common in compiled apps.

Environment

  • @tanstack/redact 0.2.3, default redact() preset
  • React 19.3.1 (control)
  • TanStack Start, Vite+ (Rolldown), @vitejs/plugin-react with React Compiler
  1. Same bug here, reached two other ways. Still present in 0.2.3.

    1. No useMemo needed: children passed through unchanged also re-render.

    let childRenders = 0;
    function Parent({ children }) {
      const [n, setN] = useState(0);
      return <div onClick={() => setN(n + 1)}>{n}{children}</div>;
    }
    function Child() { childRenders++; return null; }
    // render <Parent><Child /></Parent>, then update Parent twice
    // React 19.3.1: childRenders === 1    Redact 0.2.3: childRenders === 3

    That turns into a loop when a component in the passed-through subtree registers itself with the ancestor from a layout effect and gets fresh props from a wrapper:

    const SetItems = createContext(null);
    function Registry({ children }) {
      const [items, setItems] = useState(() => new Map());
      return <SetItems.Provider value={setItems}>{children}<span>{items.size}</span></SetItems.Provider>;
    }
    function Mounter(props) {
      const setItems = useContext(SetItems);
      useLayoutEffect(() => { setItems((prev) => new Map(prev).set("a", props)); }, [props]);
      return null;
    }
    function Content(props) { return <Mounter {...props} />; }
    // render <Registry><Content value="a" /></Registry>
    // React: Registry 2 renders, Mounter 1, done
    // Redact (dev and prod): "flushPending exceeded 50 iterations", then it keeps rendering (I stopped it at 1000)

    Same setup as yours: jsdom, Redact's dist/react/index.js and dist/dom/client.js imported directly, no compiler.

    2. It breaks Radix NavigationMenu (and with it shadcn/ui's navigation-menu).

    @radix-ui/react-navigation-menu@1.4.1 with the default viewport uses exactly the second pattern:

    • ViewportContentMounter calls onViewportContentChange(value, { ref, ...props }) from a useLayoutEffect with [props, ...] deps.
    • That does setViewportContent(prev => new Map(prev)) in NavigationMenuProvider.
    • Under React the provider's children (NavigationMenuItem → NavigationMenuContent → mounter) are the same element, so it stops there.
    • Under Redact, counting renders shows NavigationMenuItem, NavigationMenuContent and ViewportContentMounter re-rendering on every iteration, while the component that created those elements renders only a few times. NavigationMenuContent spreads fresh props into the mounter, so the effect fires again.

    In the browser, hovering or clicking a trigger throws about every 120 ms and the tab stops responding within about 3 s. The same app without withRedact opens the menu normally.

    Environment: withRedact on Next.js 16.4.3 (Turbopack, cacheComponents, React Compiler), React 19.3.1, Chromium 153. The jsdom repro above needs none of that.

  2. tannerlinsley linked this to #133

Sign in to join this conversation.