From fb31042edb60ff0b297eab5a1628ae25e419ff21 Mon Sep 17 00:00:00 2001 From: Andrew Clark Date: Sun, 5 Mar 2023 18:37:31 -0500 Subject: [PATCH] Fix: Infinite `act` loop caused by wrong `shouldYield` Fixes the bug demonstrated by the regression test in the previous commit. Refer to previous message for details. --- packages/react-reconciler/src/ReactFiberWorkLoop.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/react-reconciler/src/ReactFiberWorkLoop.js b/packages/react-reconciler/src/ReactFiberWorkLoop.js index fd6e51a0872bc..1b34601740f60 100644 --- a/packages/react-reconciler/src/ReactFiberWorkLoop.js +++ b/packages/react-reconciler/src/ReactFiberWorkLoop.js @@ -2268,7 +2268,17 @@ function renderRootConcurrent(root: FiberRoot, lanes: Lanes) { } } } - workLoopConcurrent(); + + if (__DEV__ && ReactCurrentActQueue.current !== null) { + // `act` special case: If we're inside an `act` scope, don't consult + // `shouldYield`. Always keep working until the render is complete. + // This is not just an optimization: in a unit test environment, we + // can't trust the result of `shouldYield`, because the host I/O is + // likely mocked. + workLoopSync(); + } else { + workLoopConcurrent(); + } break; } catch (thrownValue) { handleThrow(root, thrownValue);