Skip to content

Commit

Permalink
Replace passive effect context with boolean (#18309)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn committed Mar 16, 2020
1 parent 730389b commit c0ed0a2
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions packages/react-reconciler/src/ReactFiberWorkLoop.js
Expand Up @@ -199,14 +199,13 @@ const {

type ExecutionContext = number;

const NoContext = /* */ 0b0000000;
const BatchedContext = /* */ 0b0000001;
const EventContext = /* */ 0b0000010;
const DiscreteEventContext = /* */ 0b0000100;
const LegacyUnbatchedContext = /* */ 0b0001000;
const RenderContext = /* */ 0b0010000;
const CommitContext = /* */ 0b0100000;
const PassiveEffectContext = /* */ 0b1000000;
const NoContext = /* */ 0b000000;
const BatchedContext = /* */ 0b000001;
const EventContext = /* */ 0b000010;
const DiscreteEventContext = /* */ 0b000100;
const LegacyUnbatchedContext = /* */ 0b001000;
const RenderContext = /* */ 0b010000;
const CommitContext = /* */ 0b100000;

type RootExitStatus = 0 | 1 | 2 | 3 | 4 | 5;
const RootIncomplete = 0;
Expand Down Expand Up @@ -299,6 +298,10 @@ let spawnedWorkDuringRender: null | Array<ExpirationTime> = null;
// receive the same expiration time. Otherwise we get tearing.
let currentEventTime: ExpirationTime = NoWork;

// Dev only flag that tracks if passive effects are currently being flushed.
// We warn about state updates for unmounted components differently in this case.
let isFlushingPassiveEffects = false;

export function getWorkInProgressRoot(): FiberRoot | null {
return workInProgressRoot;
}
Expand Down Expand Up @@ -2273,6 +2276,10 @@ function flushPassiveEffectsImpl() {
return false;
}

if (__DEV__) {
isFlushingPassiveEffects = true;
}

const root = rootWithPendingPassiveEffects;
const expirationTime = pendingPassiveEffectsExpirationTime;
rootWithPendingPassiveEffects = null;
Expand All @@ -2284,7 +2291,6 @@ function flushPassiveEffectsImpl() {
);
const prevExecutionContext = executionContext;
executionContext |= CommitContext;
executionContext |= PassiveEffectContext;
const prevInteractions = pushInteractions(root);

if (runAllPassiveEffectDestroysBeforeCreates) {
Expand Down Expand Up @@ -2448,6 +2454,10 @@ function flushPassiveEffectsImpl() {
nestedPassiveUpdateCount =
rootWithPendingPassiveEffects === null ? 0 : nestedPassiveUpdateCount + 1;

if (__DEV__) {
isFlushingPassiveEffects = false;
}

return true;
}

Expand Down Expand Up @@ -2816,7 +2826,7 @@ function warnAboutUpdateOnUnmountedFiberInDEV(fiber) {
}

// If we are currently flushing passive effects, change the warning text.
if ((executionContext & PassiveEffectContext) !== NoContext) {
if (isFlushingPassiveEffects) {
console.error(
"Can't perform a React state update from within a useEffect cleanup function. " +
'To fix, move state updates to the useEffect() body in %s.%s',
Expand Down

0 comments on commit c0ed0a2

Please sign in to comment.