@samitouri / QOS-React / commits / 1102224bbb

Fix: flushSync changes priority inside effect (#21122)

When called from inside an effect, flushSync cannot synchronously flush its updates because React is already working. So we fire a warning. However, we should still change the priority of the updates to sync so that they flush at the end of the current task. This only affects useEffect because updates inside useLayoutEffect (and the rest of the commit phase, like ref callbacks) are already sync.

Andrew Clark committed Mar 28, 2021 at 18:50 UTC 1102224bbbfd587ec6088620c66f048d10856c59
3 files changed +79 -22
packages/react-reconciler/src/ReactFiberWorkLoop.new.js
+11 -11
@@ -1166,16 +1166,6 @@ export function unbatchedUpdates<A, R>(fn: (a: A) => R, a: A): R {
1166
1167 export function flushSync<A, R>(fn: A => R, a: A): R {
1168 const prevExecutionContext = executionContext;
1169 - if ((prevExecutionContext & (RenderContext | CommitContext)) !== NoContext) {
1170 - if (__DEV__) {
1171 - console.error(
1172 - 'flushSync was called from inside a lifecycle method. React cannot ' +
1173 - 'flush when React is already rendering. Consider moving this call to ' +
1174 - 'a scheduler task or micro task.',
1175 - );
1176 - }
1177 - return fn(a);
1178 - }
1169 executionContext |= BatchedContext;
1170
1171 const previousPriority = getCurrentUpdatePriority();
@@ -1192,7 +1182,17 @@ export function flushSync<A, R>(fn: A => R, a: A): R {
1182 // Flush the immediate callbacks that were scheduled during this batch.
1183 // Note that this will happen even if batchedUpdates is higher up
1184 // the stack.
1195 - flushSyncCallbackQueue();
1185 + if ((executionContext & (RenderContext | CommitContext)) === NoContext) {
1186 + flushSyncCallbackQueue();
1187 + } else {
1188 + if (__DEV__) {
1189 + console.error(
1190 + 'flushSync was called from inside a lifecycle method. React cannot ' +
1191 + 'flush when React is already rendering. Consider moving this call to ' +
1192 + 'a scheduler task or micro task.',
1193 + );
1194 + }
1195 + }
1196 }
1197 }
1198
packages/react-reconciler/src/ReactFiberWorkLoop.old.js
+11 -11
@@ -1166,16 +1166,6 @@ export function unbatchedUpdates<A, R>(fn: (a: A) => R, a: A): R {
1166
1167 export function flushSync<A, R>(fn: A => R, a: A): R {
1168 const prevExecutionContext = executionContext;
1169 - if ((prevExecutionContext & (RenderContext | CommitContext)) !== NoContext) {
1170 - if (__DEV__) {
1171 - console.error(
1172 - 'flushSync was called from inside a lifecycle method. React cannot ' +
1173 - 'flush when React is already rendering. Consider moving this call to ' +
1174 - 'a scheduler task or micro task.',
1175 - );
1176 - }
1177 - return fn(a);
1178 - }
1169 executionContext |= BatchedContext;
1170
1171 const previousPriority = getCurrentUpdatePriority();
@@ -1192,7 +1182,17 @@ export function flushSync<A, R>(fn: A => R, a: A): R {
1182 // Flush the immediate callbacks that were scheduled during this batch.
1183 // Note that this will happen even if batchedUpdates is higher up
1184 // the stack.
1195 - flushSyncCallbackQueue();
1185 + if ((executionContext & (RenderContext | CommitContext)) === NoContext) {
1186 + flushSyncCallbackQueue();
1187 + } else {
1188 + if (__DEV__) {
1189 + console.error(
1190 + 'flushSync was called from inside a lifecycle method. React cannot ' +
1191 + 'flush when React is already rendering. Consider moving this call to ' +
1192 + 'a scheduler task or micro task.',
1193 + );
1194 + }
1195 + }
1196 }
1197 }
1198
packages/react-reconciler/src/__tests__/ReactFlushSync-test.js new
+57
@@ -0,0 +1,57 @@
1 +let React;
2 +let ReactNoop;
3 +let Scheduler;
4 +let useState;
5 +let useEffect;
6 +
7 +describe('ReactFlushSync', () => {
8 + beforeEach(() => {
9 + jest.resetModules();
10 +
11 + React = require('react');
12 + ReactNoop = require('react-noop-renderer');
13 + Scheduler = require('scheduler');
14 + useState = React.useState;
15 + useEffect = React.useEffect;
16 + });
17 +
18 + function Text({text}) {
19 + Scheduler.unstable_yieldValue(text);
20 + return text;
21 + }
22 +
23 + test('changes priority of updates in useEffect', async () => {
24 + function App() {
25 + const [syncState, setSyncState] = useState(0);
26 + const [state, setState] = useState(0);
27 + useEffect(() => {
28 + if (syncState !== 1) {
29 + setState(1);
30 + ReactNoop.flushSync(() => setSyncState(1));
31 + }
32 + }, [syncState, state]);
33 + return <Text text={`${syncState}, ${state}`} />;
34 + }
35 +
36 + const root = ReactNoop.createRoot();
37 + await ReactNoop.act(async () => {
38 + root.render(<App />);
39 + // This will yield right before the passive effect fires
40 + expect(Scheduler).toFlushUntilNextPaint(['0, 0']);
41 +
42 + // The passive effect will schedule a sync update and a normal update.
43 + // They should commit in two separate batches. First the sync one.
44 + expect(() =>
45 + expect(Scheduler).toFlushUntilNextPaint(['1, 0']),
46 + ).toErrorDev('flushSync was called from inside a lifecycle method');
47 +
48 + // The remaining update is not sync
49 + ReactNoop.flushSync();
50 + expect(Scheduler).toHaveYielded([]);
51 +
52 + // Now flush it.
53 + expect(Scheduler).toFlushUntilNextPaint(['1, 1']);
54 + });
55 + expect(root).toMatchRenderedOutput('1, 1');
56 + });
57 +});