Infinite update loop when a cached child element's effect sets parent state (0.2.3) #120
Send to my agentSummary#
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 />)
| Runtime | Result |
|---|---|
| React 19.3.1 | Child renders once, no error |
| Redact 0.2.3 | Child 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/redact0.2.3, defaultredact()preset- React 19.3.1 (control)
- TanStack Start, Vite+ (Rolldown),
@vitejs/plugin-reactwith React Compiler
tannerlinsley linked this to #133
Sign in to join this conversation.
Same bug here, reached two other ways. Still present in 0.2.3.
1. No
useMemoneeded:childrenpassed through unchanged also re-render.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:
Same setup as yours: jsdom, Redact's
dist/react/index.jsanddist/dom/client.jsimported directly, no compiler.2. It breaks Radix
NavigationMenu(and with it shadcn/ui'snavigation-menu).@radix-ui/react-navigation-menu@1.4.1with the default viewport uses exactly the second pattern:ViewportContentMountercallsonViewportContentChange(value, { ref, ...props })from auseLayoutEffectwith[props, ...]deps.setViewportContent(prev => new Map(prev))inNavigationMenuProvider.children(NavigationMenuItem→NavigationMenuContent→ mounter) are the same element, so it stops there.NavigationMenuItem,NavigationMenuContentandViewportContentMounterre-rendering on every iteration, while the component that created those elements renders only a few times.NavigationMenuContentspreads 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
withRedactopens the menu normally.Environment:
withRedacton Next.js 16.4.3 (Turbopack,cacheComponents, React Compiler), React 19.3.1, Chromium 153. The jsdom repro above needs none of that.