Skip to content

Commit 0fb1a7b

Browse files
committed
Don't flush synchronous work if we're in the middle of a ViewTransition async sequence
1 parent bd7beca commit 0fb1a7b

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

fixtures/view-transition/src/components/Page.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, {
22
unstable_ViewTransition as ViewTransition,
33
unstable_Activity as Activity,
44
unstable_useSwipeTransition as useSwipeTransition,
5+
useLayoutEffect,
56
useEffect,
67
useState,
78
useId,
@@ -68,6 +69,16 @@ export default function Page({url, navigate}) {
6869
return () => clearInterval(timer);
6970
}, []);
7071

72+
useLayoutEffect(() => {
73+
// Calling a default update should not interrupt ViewTransitions but
74+
// a flushSync will.
75+
// Promise.resolve().then(() => {
76+
// flushSync(() => {
77+
setCounter(c => c + 10);
78+
// });
79+
// });
80+
}, [show]);
81+
7182
const exclamation = (
7283
<ViewTransition name="exclamation" onShare={onTransition}>
7384
<span>!</span>

packages/react-reconciler/src/ReactFiberRootScheduler.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
hasPendingCommitEffects,
4949
isWorkLoopSuspendedOnData,
5050
performWorkOnRoot,
51+
isPreparingViewTransition,
5152
} from './ReactFiberWorkLoop';
5253
import {LegacyRoot} from './ReactRootTags';
5354
import {
@@ -310,7 +311,12 @@ function processRootScheduleInMicrotask() {
310311

311312
// At the end of the microtask, flush any pending synchronous work. This has
312313
// to come at the end, because it does actual rendering work that might throw.
313-
flushSyncWorkAcrossRoots_impl(syncTransitionLanes, false);
314+
// If we're in the middle of a View Transition async sequence, we don't want to
315+
// interrupt that sequence. Instead, we'll flush any remaining work when it
316+
// completes.
317+
if (!isPreparingViewTransition()) {
318+
flushSyncWorkAcrossRoots_impl(syncTransitionLanes, false);
319+
}
314320
}
315321

316322
function scheduleTaskForRootDuringMicrotask(

packages/react-reconciler/src/ReactFiberWorkLoop.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,10 @@ let pendingTransitionTypes: null | TransitionTypes = null;
677677
let pendingDidIncludeRenderPhaseUpdate: boolean = false;
678678
let pendingSuspendedCommitReason: SuspendedCommitReason = IMMEDIATE_COMMIT; // Profiling-only
679679

680+
export function isPreparingViewTransition(): boolean {
681+
return enableViewTransition && pendingViewTransition !== null;
682+
}
683+
680684
// Use these to prevent an infinite loop of nested updates
681685
const NESTED_UPDATE_LIMIT = 50;
682686
let nestedUpdateCount: number = 0;

0 commit comments

Comments
 (0)