@samitouri / QOS-React / commits / c0ed0a2468

Replace passive effect context with boolean (#18309)

Brian Vaughn committed Mar 16, 2020 at 09:56 UTC c0ed0a2468d8f4fdecc42e5fe36ef5a4d1dcb68b
1 file changed +20 -10
packages/react-reconciler/src/ReactFiberWorkLoop.js
+20 -10
@@ -199,14 +199,13 @@ const {
199
200 type ExecutionContext = number;
201
202 -const NoContext = /* */ 0b0000000;
203 -const BatchedContext = /* */ 0b0000001;
204 -const EventContext = /* */ 0b0000010;
205 -const DiscreteEventContext = /* */ 0b0000100;
206 -const LegacyUnbatchedContext = /* */ 0b0001000;
207 -const RenderContext = /* */ 0b0010000;
208 -const CommitContext = /* */ 0b0100000;
209 -const PassiveEffectContext = /* */ 0b1000000;
202 +const NoContext = /* */ 0b000000;
203 +const BatchedContext = /* */ 0b000001;
204 +const EventContext = /* */ 0b000010;
205 +const DiscreteEventContext = /* */ 0b000100;
206 +const LegacyUnbatchedContext = /* */ 0b001000;
207 +const RenderContext = /* */ 0b010000;
208 +const CommitContext = /* */ 0b100000;
209
210 type RootExitStatus = 0 | 1 | 2 | 3 | 4 | 5;
211 const RootIncomplete = 0;
@@ -299,6 +298,10 @@ let spawnedWorkDuringRender: null | Array<ExpirationTime> = null;
298 // receive the same expiration time. Otherwise we get tearing.
299 let currentEventTime: ExpirationTime = NoWork;
300
301 +// Dev only flag that tracks if passive effects are currently being flushed.
302 +// We warn about state updates for unmounted components differently in this case.
303 +let isFlushingPassiveEffects = false;
304 +
305 export function getWorkInProgressRoot(): FiberRoot | null {
306 return workInProgressRoot;
307 }
@@ -2273,6 +2276,10 @@ function flushPassiveEffectsImpl() {
2276 return false;
2277 }
2278
2279 + if (__DEV__) {
2280 + isFlushingPassiveEffects = true;
2281 + }
2282 +
2283 const root = rootWithPendingPassiveEffects;
2284 const expirationTime = pendingPassiveEffectsExpirationTime;
2285 rootWithPendingPassiveEffects = null;
@@ -2284,7 +2291,6 @@ function flushPassiveEffectsImpl() {
2291 );
2292 const prevExecutionContext = executionContext;
2293 executionContext |= CommitContext;
2287 - executionContext |= PassiveEffectContext;
2294 const prevInteractions = pushInteractions(root);
2295
2296 if (runAllPassiveEffectDestroysBeforeCreates) {
@@ -2448,6 +2454,10 @@ function flushPassiveEffectsImpl() {
2454 nestedPassiveUpdateCount =
2455 rootWithPendingPassiveEffects === null ? 0 : nestedPassiveUpdateCount + 1;
2456
2457 + if (__DEV__) {
2458 + isFlushingPassiveEffects = false;
2459 + }
2460 +
2461 return true;
2462 }
2463
@@ -2816,7 +2826,7 @@ function warnAboutUpdateOnUnmountedFiberInDEV(fiber) {
2826 }
2827
2828 // If we are currently flushing passive effects, change the warning text.
2819 - if ((executionContext & PassiveEffectContext) !== NoContext) {
2829 + if (isFlushingPassiveEffects) {
2830 console.error(
2831 "Can't perform a React state update from within a useEffect cleanup function. " +
2832 'To fix, move state updates to the useEffect() body in %s.%s',