Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align StrictMode behaviour with production #25049

Merged
merged 12 commits into from
Aug 23, 2022
13 changes: 9 additions & 4 deletions packages/react-reconciler/src/ReactChildFiber.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import type {Fiber} from './ReactInternalTypes';
import type {Lanes} from './ReactFiberLane.new';

import getComponentNameFromFiber from 'react-reconciler/src/getComponentNameFromFiber';
import {Placement, ChildDeletion, Forked} from './ReactFiberFlags';
import {
Placement,
ChildDeletion,
Forked,
PlacementDEV,
} from './ReactFiberFlags';
import {
getIteratorFn,
REACT_ELEMENT_TYPE,
Expand Down Expand Up @@ -343,15 +348,15 @@ function ChildReconciler(shouldTrackSideEffects) {
const oldIndex = current.index;
if (oldIndex < lastPlacedIndex) {
// This is a move.
newFiber.flags |= Placement;
newFiber.flags |= Placement | PlacementDEV;
return lastPlacedIndex;
} else {
// This item can stay in place.
return oldIndex;
}
} else {
// This is an insertion.
newFiber.flags |= Placement;
newFiber.flags |= Placement | PlacementDEV;
return lastPlacedIndex;
}
}
Expand All @@ -360,7 +365,7 @@ function ChildReconciler(shouldTrackSideEffects) {
// This is simpler for the single child case. We only need to do a
// placement for inserting new children.
if (shouldTrackSideEffects && newFiber.alternate === null) {
newFiber.flags |= Placement;
newFiber.flags |= Placement | PlacementDEV;
}
return newFiber;
}
Expand Down
13 changes: 9 additions & 4 deletions packages/react-reconciler/src/ReactChildFiber.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ import type {Fiber} from './ReactInternalTypes';
import type {Lanes} from './ReactFiberLane.old';

import getComponentNameFromFiber from 'react-reconciler/src/getComponentNameFromFiber';
import {Placement, ChildDeletion, Forked} from './ReactFiberFlags';
import {
Placement,
ChildDeletion,
Forked,
PlacementDEV,
} from './ReactFiberFlags';
import {
getIteratorFn,
REACT_ELEMENT_TYPE,
Expand Down Expand Up @@ -343,15 +348,15 @@ function ChildReconciler(shouldTrackSideEffects) {
const oldIndex = current.index;
if (oldIndex < lastPlacedIndex) {
// This is a move.
newFiber.flags |= Placement;
newFiber.flags |= Placement | PlacementDEV;
return lastPlacedIndex;
} else {
// This item can stay in place.
return oldIndex;
}
} else {
// This is an insertion.
newFiber.flags |= Placement;
newFiber.flags |= Placement | PlacementDEV;
return lastPlacedIndex;
}
}
Expand All @@ -360,7 +365,7 @@ function ChildReconciler(shouldTrackSideEffects) {
// This is simpler for the single child case. We only need to do a
// placement for inserting new children.
if (shouldTrackSideEffects && newFiber.alternate === null) {
newFiber.flags |= Placement;
newFiber.flags |= Placement | PlacementDEV;
}
return newFiber;
}
Expand Down
165 changes: 37 additions & 128 deletions packages/react-reconciler/src/ReactFiberCommitWork.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import {
enableSchedulingProfiler,
enableSuspenseCallback,
enableScopeAPI,
enableStrictEffects,
deletedTreeCleanUpLevel,
enableUpdaterTracking,
enableCache,
Expand Down Expand Up @@ -148,6 +147,8 @@ import {
addMarkerProgressCallbackToPendingTransition,
addMarkerCompleteCallbackToPendingTransition,
setIsRunningInsertionEffect,
getExecutionContext,
CommitContext,
} from './ReactFiberWorkLoop.new';
import {
NoFlags as NoHookEffect,
Expand Down Expand Up @@ -220,7 +221,8 @@ const callComponentWillUnmountWithTimer = function(current, instance) {
if (
enableProfilerTimer &&
enableProfilerCommitHooks &&
current.mode & ProfileMode
current.mode & ProfileMode &&
getExecutionContext() & CommitContext
) {
try {
startLayoutEffectTimer();
Expand Down Expand Up @@ -264,7 +266,8 @@ function safelyDetachRef(current: Fiber, nearestMountedAncestor: Fiber | null) {
if (
enableProfilerTimer &&
enableProfilerCommitHooks &&
current.mode & ProfileMode
current.mode & ProfileMode &&
getExecutionContext() & CommitContext
) {
try {
startLayoutEffectTimer();
Expand Down Expand Up @@ -641,7 +644,11 @@ export function commitPassiveEffectDurations(
finishedRoot: FiberRoot,
finishedWork: Fiber,
): void {
if (enableProfilerTimer && enableProfilerCommitHooks) {
if (
enableProfilerTimer &&
enableProfilerCommitHooks &&
getExecutionContext() & CommitContext
) {
// Only Profilers with work in their subtree will have an Update effect scheduled.
if ((finishedWork.flags & Update) !== NoFlags) {
switch (finishedWork.tag) {
Expand Down Expand Up @@ -697,7 +704,8 @@ function commitHookLayoutEffects(finishedWork: Fiber, hookFlags: HookFlags) {
if (
enableProfilerTimer &&
enableProfilerCommitHooks &&
finishedWork.mode & ProfileMode
finishedWork.mode & ProfileMode &&
getExecutionContext() & CommitContext
) {
try {
startLayoutEffectTimer();
Expand Down Expand Up @@ -754,7 +762,8 @@ function commitClassLayoutLifecycles(
if (
enableProfilerTimer &&
enableProfilerCommitHooks &&
finishedWork.mode & ProfileMode
finishedWork.mode & ProfileMode &&
getExecutionContext() & CommitContext
sammy-SC marked this conversation as resolved.
Show resolved Hide resolved
) {
try {
startLayoutEffectTimer();
Expand Down Expand Up @@ -809,7 +818,8 @@ function commitClassLayoutLifecycles(
if (
enableProfilerTimer &&
enableProfilerCommitHooks &&
finishedWork.mode & ProfileMode
finishedWork.mode & ProfileMode &&
getExecutionContext() & CommitContext
) {
try {
startLayoutEffectTimer();
Expand Down Expand Up @@ -892,7 +902,7 @@ function commitHostComponentMount(finishedWork: Fiber) {
}

function commitProfilerUpdate(finishedWork: Fiber, current: Fiber | null) {
if (enableProfilerTimer) {
if (enableProfilerTimer && getExecutionContext() & CommitContext) {
try {
const {onCommit, onRender} = finishedWork.memoizedProps;
const {effectDuration} = finishedWork.stateNode;
Expand Down Expand Up @@ -1344,7 +1354,8 @@ function commitAttachRef(finishedWork: Fiber) {
if (
enableProfilerTimer &&
enableProfilerCommitHooks &&
finishedWork.mode & ProfileMode
finishedWork.mode & ProfileMode &&
getExecutionContext() & CommitContext
) {
try {
startLayoutEffectTimer();
Expand Down Expand Up @@ -1387,7 +1398,8 @@ function commitDetachRef(current: Fiber) {
if (
enableProfilerTimer &&
enableProfilerCommitHooks &&
current.mode & ProfileMode
current.mode & ProfileMode &&
getExecutionContext() & CommitContext
) {
try {
startLayoutEffectTimer();
Expand Down Expand Up @@ -1917,7 +1929,8 @@ function commitDeletionEffectsOnFiber(
if (
enableProfilerTimer &&
enableProfilerCommitHooks &&
deletedFiber.mode & ProfileMode
deletedFiber.mode & ProfileMode &&
getExecutionContext() & CommitContext
) {
startLayoutEffectTimer();
safelyCallDestroy(
Expand Down Expand Up @@ -2240,7 +2253,8 @@ function commitMutationEffectsOnFiber(
if (
enableProfilerTimer &&
enableProfilerCommitHooks &&
finishedWork.mode & ProfileMode
finishedWork.mode & ProfileMode &&
getExecutionContext() & CommitContext
) {
try {
startLayoutEffectTimer();
Expand Down Expand Up @@ -2627,7 +2641,7 @@ function recursivelyTraverseLayoutEffects(
setCurrentDebugFiberInDEV(prevDebugFiber);
}

function disappearLayoutEffects(finishedWork: Fiber) {
export function disappearLayoutEffects(finishedWork: Fiber) {
switch (finishedWork.tag) {
case FunctionComponent:
case ForwardRef:
Expand All @@ -2637,7 +2651,8 @@ function disappearLayoutEffects(finishedWork: Fiber) {
if (
enableProfilerTimer &&
enableProfilerCommitHooks &&
finishedWork.mode & ProfileMode
finishedWork.mode & ProfileMode &&
getExecutionContext() & CommitContext
) {
try {
startLayoutEffectTimer();
Expand Down Expand Up @@ -2709,7 +2724,7 @@ function recursivelyTraverseDisappearLayoutEffects(parentFiber: Fiber) {
}
}

function reappearLayoutEffects(
export function reappearLayoutEffects(
finishedRoot: FiberRoot,
current: Fiber | null,
finishedWork: Fiber,
Expand Down Expand Up @@ -2879,7 +2894,8 @@ function commitHookPassiveMountEffects(
if (
enableProfilerTimer &&
enableProfilerCommitHooks &&
finishedWork.mode & ProfileMode
finishedWork.mode & ProfileMode &&
getExecutionContext() & CommitContext
) {
startPassiveEffectTimer();
try {
Expand Down Expand Up @@ -3304,7 +3320,7 @@ function recursivelyTraverseReconnectPassiveEffects(
setCurrentDebugFiberInDEV(prevDebugFiber);
}

function reconnectPassiveEffects(
export function reconnectPassiveEffects(
finishedRoot: FiberRoot,
finishedWork: Fiber,
committedLanes: Lanes,
Expand Down Expand Up @@ -3588,7 +3604,8 @@ function commitHookPassiveUnmountEffects(
if (
enableProfilerTimer &&
enableProfilerCommitHooks &&
finishedWork.mode & ProfileMode
finishedWork.mode & ProfileMode &&
getExecutionContext() & CommitContext
) {
startPassiveEffectTimer();
commitHookEffectListUnmount(
Expand Down Expand Up @@ -3718,7 +3735,7 @@ function recursivelyTraverseDisconnectPassiveEffects(parentFiber: Fiber): void {
setCurrentDebugFiberInDEV(prevDebugFiber);
}

function disconnectPassiveEffect(finishedWork: Fiber): void {
export function disconnectPassiveEffect(finishedWork: Fiber): void {
switch (finishedWork.tag) {
case FunctionComponent:
case ForwardRef:
Expand Down Expand Up @@ -3870,112 +3887,4 @@ function commitPassiveUnmountInsideDeletedTreeOnFiber(
}
}

// TODO: Reuse reappearLayoutEffects traversal here?
function invokeLayoutEffectMountInDEV(fiber: Fiber): void {
if (__DEV__ && enableStrictEffects) {
// We don't need to re-check StrictEffectsMode here.
// This function is only called if that check has already passed.
switch (fiber.tag) {
case FunctionComponent:
case ForwardRef:
case SimpleMemoComponent: {
try {
commitHookEffectListMount(HookLayout | HookHasEffect, fiber);
} catch (error) {
captureCommitPhaseError(fiber, fiber.return, error);
}
break;
}
case ClassComponent: {
const instance = fiber.stateNode;
try {
instance.componentDidMount();
} catch (error) {
captureCommitPhaseError(fiber, fiber.return, error);
}
break;
}
}
}
}

function invokePassiveEffectMountInDEV(fiber: Fiber): void {
if (__DEV__ && enableStrictEffects) {
// We don't need to re-check StrictEffectsMode here.
// This function is only called if that check has already passed.
switch (fiber.tag) {
case FunctionComponent:
case ForwardRef:
case SimpleMemoComponent: {
try {
commitHookEffectListMount(HookPassive | HookHasEffect, fiber);
} catch (error) {
captureCommitPhaseError(fiber, fiber.return, error);
}
break;
}
}
}
}

function invokeLayoutEffectUnmountInDEV(fiber: Fiber): void {
if (__DEV__ && enableStrictEffects) {
// We don't need to re-check StrictEffectsMode here.
// This function is only called if that check has already passed.
switch (fiber.tag) {
case FunctionComponent:
case ForwardRef:
case SimpleMemoComponent: {
try {
commitHookEffectListUnmount(
HookLayout | HookHasEffect,
fiber,
fiber.return,
);
} catch (error) {
captureCommitPhaseError(fiber, fiber.return, error);
}
break;
}
case ClassComponent: {
const instance = fiber.stateNode;
if (typeof instance.componentWillUnmount === 'function') {
safelyCallComponentWillUnmount(fiber, fiber.return, instance);
}
break;
}
}
}
}

function invokePassiveEffectUnmountInDEV(fiber: Fiber): void {
if (__DEV__ && enableStrictEffects) {
// We don't need to re-check StrictEffectsMode here.
// This function is only called if that check has already passed.
switch (fiber.tag) {
case FunctionComponent:
case ForwardRef:
case SimpleMemoComponent: {
try {
commitHookEffectListUnmount(
HookPassive | HookHasEffect,
fiber,
fiber.return,
);
} catch (error) {
captureCommitPhaseError(fiber, fiber.return, error);
}
}
}
}
}

export {
commitPlacement,
commitAttachRef,
commitDetachRef,
invokeLayoutEffectMountInDEV,
invokeLayoutEffectUnmountInDEV,
invokePassiveEffectMountInDEV,
invokePassiveEffectUnmountInDEV,
};
export {commitPlacement, commitAttachRef, commitDetachRef};
Loading