Skip to content

Commit

Permalink
Revert "Fix: flushSync changes priority inside effect (facebook#21122)"
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
acdlite authored and koto committed Jun 15, 2021
1 parent 5b5024d commit d03ed9d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 86 deletions.
22 changes: 11 additions & 11 deletions packages/react-reconciler/src/ReactFiberWorkLoop.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,16 @@ export function unbatchedUpdates<A, R>(fn: (a: A) => R, a: A): R {

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

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

Expand Down
22 changes: 11 additions & 11 deletions packages/react-reconciler/src/ReactFiberWorkLoop.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,16 @@ export function unbatchedUpdates<A, R>(fn: (a: A) => R, a: A): R {

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

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

Expand Down
64 changes: 0 additions & 64 deletions packages/react-reconciler/src/__tests__/ReactFlushSync-test.js

This file was deleted.

0 comments on commit d03ed9d

Please sign in to comment.