Skip to content

Commit d51fff3

Browse files
committed
Fix batching
1 parent 60d5b3e commit d51fff3

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

packages/react-dom/src/__tests__/ReactDOMHooks-test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,42 @@ describe('ReactDOMHooks', () => {
7070
expect(container3.textContent).toBe('6');
7171
});
7272

73+
it('can batch synchronous work inside effects with other work', () => {
74+
let otherContainer = document.createElement('div');
75+
76+
let calledA = false;
77+
function A() {
78+
calledA = true;
79+
return 'A';
80+
}
81+
82+
let calledB = false;
83+
function B() {
84+
calledB = true;
85+
return 'B';
86+
}
87+
88+
let _set;
89+
function Foo() {
90+
_set = React.useState(0)[1];
91+
React.useEffect(() => {
92+
ReactDOM.render(<A />, otherContainer);
93+
});
94+
return null;
95+
}
96+
97+
ReactDOM.render(<Foo />, container);
98+
ReactDOM.unstable_batchedUpdates(() => {
99+
_set(0); // Forces the effect to be flushed
100+
expect(otherContainer.textContent).toBe('');
101+
ReactDOM.render(<B />, otherContainer);
102+
expect(otherContainer.textContent).toBe('');
103+
});
104+
expect(otherContainer.textContent).toBe('B');
105+
expect(calledA).toBe(false); // It was in a batch
106+
expect(calledB).toBe(true);
107+
});
108+
73109
it('should not bail out when an update is scheduled from within an event handler', () => {
74110
const {createRef, useCallback, useState} = React;
75111

packages/react-reconciler/src/ReactFiberScheduler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ function commitPassiveEffects(root: FiberRoot, firstEffect: Fiber): void {
573573
requestWork(root, rootExpirationTime);
574574
}
575575
// Flush any sync work that was scheduled by effects
576-
if (!isRendering) {
576+
if (!isBatchingUpdates && !isRendering) {
577577
performSyncWork();
578578
}
579579
}

0 commit comments

Comments
 (0)