Skip to content

Commit e7e0a90

Browse files
committed
Revert "Fix: flushSync changes priority inside effect (#21122)"
This reverts commit 0e3c7e1. 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.
1 parent 7bac760 commit e7e0a90

File tree

3 files changed

+22
-86
lines changed

3 files changed

+22
-86
lines changed

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

+11-11
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,16 @@ export function unbatchedUpdates<A, R>(fn: (a: A) => R, a: A): R {
11421142

11431143
export function flushSync<A, R>(fn: A => R, a: A): R {
11441144
const prevExecutionContext = executionContext;
1145+
if ((prevExecutionContext & (RenderContext | CommitContext)) !== NoContext) {
1146+
if (__DEV__) {
1147+
console.error(
1148+
'flushSync was called from inside a lifecycle method. React cannot ' +
1149+
'flush when React is already rendering. Consider moving this call to ' +
1150+
'a scheduler task or micro task.',
1151+
);
1152+
}
1153+
return fn(a);
1154+
}
11451155
executionContext |= BatchedContext;
11461156

11471157
const previousPriority = getCurrentUpdatePriority();
@@ -1158,17 +1168,7 @@ export function flushSync<A, R>(fn: A => R, a: A): R {
11581168
// Flush the immediate callbacks that were scheduled during this batch.
11591169
// Note that this will happen even if batchedUpdates is higher up
11601170
// the stack.
1161-
if ((executionContext & (RenderContext | CommitContext)) === NoContext) {
1162-
flushSyncCallbacks();
1163-
} else {
1164-
if (__DEV__) {
1165-
console.error(
1166-
'flushSync was called from inside a lifecycle method. React cannot ' +
1167-
'flush when React is already rendering. Consider moving this call to ' +
1168-
'a scheduler task or micro task.',
1169-
);
1170-
}
1171-
}
1171+
flushSyncCallbacks();
11721172
}
11731173
}
11741174

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

+11-11
Original file line numberDiff line numberDiff line change
@@ -1142,6 +1142,16 @@ export function unbatchedUpdates<A, R>(fn: (a: A) => R, a: A): R {
11421142

11431143
export function flushSync<A, R>(fn: A => R, a: A): R {
11441144
const prevExecutionContext = executionContext;
1145+
if ((prevExecutionContext & (RenderContext | CommitContext)) !== NoContext) {
1146+
if (__DEV__) {
1147+
console.error(
1148+
'flushSync was called from inside a lifecycle method. React cannot ' +
1149+
'flush when React is already rendering. Consider moving this call to ' +
1150+
'a scheduler task or micro task.',
1151+
);
1152+
}
1153+
return fn(a);
1154+
}
11451155
executionContext |= BatchedContext;
11461156

11471157
const previousPriority = getCurrentUpdatePriority();
@@ -1158,17 +1168,7 @@ export function flushSync<A, R>(fn: A => R, a: A): R {
11581168
// Flush the immediate callbacks that were scheduled during this batch.
11591169
// Note that this will happen even if batchedUpdates is higher up
11601170
// the stack.
1161-
if ((executionContext & (RenderContext | CommitContext)) === NoContext) {
1162-
flushSyncCallbacks();
1163-
} else {
1164-
if (__DEV__) {
1165-
console.error(
1166-
'flushSync was called from inside a lifecycle method. React cannot ' +
1167-
'flush when React is already rendering. Consider moving this call to ' +
1168-
'a scheduler task or micro task.',
1169-
);
1170-
}
1171-
}
1171+
flushSyncCallbacks();
11721172
}
11731173
}
11741174

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

-64
This file was deleted.

0 commit comments

Comments
 (0)