fix: resolve infinite update loop when using createRoot (React 18)#367
Open
zerosrat wants to merge 1 commit intoCJY0208:masterfrom
Open
fix: resolve infinite update loop when using createRoot (React 18)#367zerosrat wants to merge 1 commit intoCJY0208:masterfrom
zerosrat wants to merge 1 commit intoCJY0208:masterfrom
Conversation
When using React 18's `createRoot` instead of `ReactDOM.render`, two code paths cause "Maximum update depth exceeded" errors: 1. FallbackListener.componentDidMount calls setState synchronously, creating a tight loop: componentDidMount → setState → re-render → throw promise → Suspense fallback → componentDidMount → ... Fix: defer onStart to a microtask with Promise.resolve().then(), breaking the synchronous cycle. Added unmount guard to prevent setState after unmount. 2. Keeper uses flushSync to set freeze state, which forces synchronous rendering inside createRoot and bypasses React 18's automatic batching, amplifying the infinite loop. Fix: remove flushSync wrapper — the setTimeout already provides the necessary delay. Closes CJY0208#336, Closes CJY0208#257 Co-Authored-By: Claude Opus 4 <noreply@anthropic.com>
aecc90d to
c81a4d5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When using React 18's
createRoot(instead of legacyReactDOM.render), switching between cached KeepAlive components triggers "Maximum update depth exceeded" errors. This makesreact-activationincompatible withcreateRoot.Related issues: #336, #257
Root Cause
Two code paths create infinite synchronous render loops under
createRoot:1.
FallbackListener.componentDidMount(Suspense.js)componentDidMountcallsonStart→setState({ suspense: true })synchronously, creating a tight loop:In React 17 / legacy
ReactDOM.render, batching behavior prevented this from becoming an infinite loop. WithcreateRoot, React 18's automatic batching works differently for Suspense fallback rendering, making the cycle synchronous and hitting the max update depth.2.
KeeperusesflushSync(Keeper.js)flushSyncforces synchronous rendering insidecreateRoot, bypassing React 18's automatic batching and amplifying the infinite loop.Fix
Suspense.js: Defer
onStartcallback to a microtask usingPromise.resolve().then(), breaking the synchronous cycle. Added an_unmountedguard to prevent calling setState after unmount.Keeper.js: Remove
flushSyncwrapper around the freezesetStatecall. ThesetTimeout(..., 1000)already provides the necessary delay for post-cache processing.Testing
Tested with:
react@18.3.1+react-dom@18.3.1react-activation@0.13.4createRootwithAliveScope+KeepAlive🤖 Generated with Claude Code