Skip to content

Commit 12a4f0b

Browse files
committed
Flush legacy passive effects at beginning of event
Fixes test added in previous commit.
1 parent 1a63add commit 12a4f0b

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

packages/react-reconciler/src/ReactFiberWorkLoop.new.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,15 @@ export function flushSyncWithoutWarningIfAlreadyRendering<A, R>(
10821082
fn: A => R,
10831083
a: A,
10841084
): R {
1085+
// In legacy mode, we flush pending passive effects at the beginning of the
1086+
// next event, not at the end of the previous one.
1087+
if (
1088+
rootWithPendingPassiveEffects !== null &&
1089+
rootWithPendingPassiveEffects.tag === LegacyRoot
1090+
) {
1091+
flushPassiveEffects();
1092+
}
1093+
10851094
const prevExecutionContext = executionContext;
10861095
executionContext |= BatchedContext;
10871096

packages/react-reconciler/src/ReactFiberWorkLoop.old.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,15 @@ export function flushSyncWithoutWarningIfAlreadyRendering<A, R>(
10821082
fn: A => R,
10831083
a: A,
10841084
): R {
1085+
// In legacy mode, we flush pending passive effects at the beginning of the
1086+
// next event, not at the end of the previous one.
1087+
if (
1088+
rootWithPendingPassiveEffects !== null &&
1089+
rootWithPendingPassiveEffects.tag === LegacyRoot
1090+
) {
1091+
flushPassiveEffects();
1092+
}
1093+
10851094
const prevExecutionContext = executionContext;
10861095
executionContext |= BatchedContext;
10871096

packages/react-reconciler/src/__tests__/ReactHooksWithNoopRenderer-test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,10 +1795,11 @@ describe('ReactHooksWithNoopRenderer', () => {
17951795
return <Text text={'Count: ' + count} />;
17961796
}
17971797
await act(async () => {
1798-
ReactNoop.renderLegacySyncRoot(<Counter count={0} />);
1798+
ReactNoop.flushSync(() => {
1799+
ReactNoop.renderLegacySyncRoot(<Counter count={0} />);
1800+
});
17991801

18001802
// Even in legacy mode, effects are deferred until after paint
1801-
ReactNoop.flushSync();
18021803
expect(Scheduler).toHaveYielded(['Count: (empty)']);
18031804
expect(ReactNoop.getChildren()).toEqual([span('Count: (empty)')]);
18041805
});

0 commit comments

Comments
 (0)