diff --git a/packages/react-reconciler/src/ReactFiberWorkLoop.js b/packages/react-reconciler/src/ReactFiberWorkLoop.js index d24ece936b2df..7e2e8e0cef04b 100644 --- a/packages/react-reconciler/src/ReactFiberWorkLoop.js +++ b/packages/react-reconciler/src/ReactFiberWorkLoop.js @@ -1782,6 +1782,19 @@ function commitRootImpl(root, renderPriorityLevel) { remainingExpirationTimeBeforeCommit, ); + // Clear already finished discrete updates in case that a later call of + // `flushDiscreteUpdates` starts a useless render pass which may cancels + // a scheduled timeout. + if (rootsWithPendingDiscreteUpdates !== null) { + const lastDiscreteTime = rootsWithPendingDiscreteUpdates.get(root); + if ( + lastDiscreteTime !== undefined && + remainingExpirationTimeBeforeCommit < lastDiscreteTime + ) { + rootsWithPendingDiscreteUpdates.delete(root); + } + } + if (root === workInProgressRoot) { // We can reset these now that they are finished. workInProgressRoot = null; diff --git a/packages/react-reconciler/src/__tests__/ReactSuspenseWithNoopRenderer-test.internal.js b/packages/react-reconciler/src/__tests__/ReactSuspenseWithNoopRenderer-test.internal.js index 272533c281c49..966fc653ca127 100644 --- a/packages/react-reconciler/src/__tests__/ReactSuspenseWithNoopRenderer-test.internal.js +++ b/packages/react-reconciler/src/__tests__/ReactSuspenseWithNoopRenderer-test.internal.js @@ -3634,7 +3634,6 @@ describe('ReactSuspenseWithNoopRenderer', () => { // Triggers erstwhile bug where flushDiscreteUpdates caused an empty render // at a previously committed level ReactNoop.flushDiscreteUpdates(); - expect(Scheduler).toFlushAndYield(['A', 'Suspend! [B]', 'Loading...']); // Commit the placeholder Scheduler.unstable_advanceTime(2000);