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

Always trigger componentWillUnmount in StrictMode #26842

Merged
merged 1 commit into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions packages/react-reconciler/src/ReactFiberClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import type {Fiber} from './ReactInternalTypes';
import type {Lanes} from './ReactFiberLane';
import type {UpdateQueue} from './ReactFiberClassUpdateQueue';
import type {Flags} from './ReactFiberFlags';

import {
LayoutStatic,
Expand Down Expand Up @@ -897,11 +896,10 @@ function mountClassInstance(
}

if (typeof instance.componentDidMount === 'function') {
let fiberFlags: Flags = Update | LayoutStatic;
if (__DEV__ && (workInProgress.mode & StrictEffectsMode) !== NoMode) {
fiberFlags |= MountLayoutDev;
}
workInProgress.flags |= fiberFlags;
workInProgress.flags |= Update | LayoutStatic;
}
if (__DEV__ && (workInProgress.mode & StrictEffectsMode) !== NoMode) {
workInProgress.flags |= MountLayoutDev;
}
}

Expand Down Expand Up @@ -971,11 +969,10 @@ function resumeMountClassInstance(
// If an update was already in progress, we should schedule an Update
// effect even though we're bailing out, so that cWU/cDU are called.
if (typeof instance.componentDidMount === 'function') {
let fiberFlags: Flags = Update | LayoutStatic;
if (__DEV__ && (workInProgress.mode & StrictEffectsMode) !== NoMode) {
fiberFlags |= MountLayoutDev;
}
workInProgress.flags |= fiberFlags;
workInProgress.flags |= Update | LayoutStatic;
}
if (__DEV__ && (workInProgress.mode & StrictEffectsMode) !== NoMode) {
workInProgress.flags |= MountLayoutDev;
}
return false;
}
Expand Down Expand Up @@ -1018,21 +1015,19 @@ function resumeMountClassInstance(
}
}
if (typeof instance.componentDidMount === 'function') {
let fiberFlags: Flags = Update | LayoutStatic;
if (__DEV__ && (workInProgress.mode & StrictEffectsMode) !== NoMode) {
fiberFlags |= MountLayoutDev;
}
workInProgress.flags |= fiberFlags;
workInProgress.flags |= Update | LayoutStatic;
}
if (__DEV__ && (workInProgress.mode & StrictEffectsMode) !== NoMode) {
workInProgress.flags |= MountLayoutDev;
}
} else {
// If an update was already in progress, we should schedule an Update
// effect even though we're bailing out, so that cWU/cDU are called.
if (typeof instance.componentDidMount === 'function') {
let fiberFlags: Flags = Update | LayoutStatic;
if (__DEV__ && (workInProgress.mode & StrictEffectsMode) !== NoMode) {
fiberFlags |= MountLayoutDev;
}
workInProgress.flags |= fiberFlags;
workInProgress.flags |= Update | LayoutStatic;
}
if (__DEV__ && (workInProgress.mode & StrictEffectsMode) !== NoMode) {
workInProgress.flags |= MountLayoutDev;
}

// If shouldComponentUpdate returned false, we should still update the
Expand Down
10 changes: 6 additions & 4 deletions packages/react-reconciler/src/ReactFiberCommitWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -4572,10 +4572,12 @@ function invokeLayoutEffectMountInDEV(fiber: Fiber): void {
}
case ClassComponent: {
const instance = fiber.stateNode;
try {
instance.componentDidMount();
} catch (error) {
captureCommitPhaseError(fiber, fiber.return, error);
if (typeof instance.componentDidMount === 'function') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR always sets MountLayoutDev on mount regardless if instance.componentDidMount exists, and when the flag is set, this code path always runs.

try {
instance.componentDidMount();
} catch (error) {
captureCommitPhaseError(fiber, fiber.return, error);
}
}
break;
}
Expand Down
116 changes: 116 additions & 0 deletions packages/react-reconciler/src/__tests__/StrictEffectsMode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,47 @@ describe('StrictEffectsMode', () => {
assertLog(['componentWillUnmount']);
});

it('invokes componentWillUnmount for class components without componentDidMount', async () => {
class App extends React.PureComponent {
componentDidUpdate() {
Scheduler.log('componentDidUpdate');
}

componentWillUnmount() {
Scheduler.log('componentWillUnmount');
}

render() {
return this.props.text;
}
}

let renderer;
await act(() => {
renderer = ReactTestRenderer.create(<App text={'mount'} />, {
unstable_isConcurrent: true,
});
});

if (supportsDoubleInvokeEffects()) {
assertLog(['componentWillUnmount']);
} else {
assertLog([]);
}

await act(() => {
renderer.update(<App text={'update'} />);
});

assertLog(['componentDidUpdate']);

await act(() => {
renderer.unmount();
});

assertLog(['componentWillUnmount']);
});

it('should not double invoke class lifecycles in legacy mode', async () => {
class App extends React.PureComponent {
componentDidMount() {
Expand Down Expand Up @@ -590,4 +631,79 @@ describe('StrictEffectsMode', () => {
'useEffect unmount',
]);
});

it('classes without componentDidMount and functions are double invoked together correctly', async () => {
class ClassChild extends React.PureComponent {
componentWillUnmount() {
Scheduler.log('componentWillUnmount');
}

render() {
return this.props.text;
}
}

function FunctionChild({text}) {
React.useEffect(() => {
Scheduler.log('useEffect mount');
return () => Scheduler.log('useEffect unmount');
});
React.useLayoutEffect(() => {
Scheduler.log('useLayoutEffect mount');
return () => Scheduler.log('useLayoutEffect unmount');
});
return text;
}

function App({text}) {
return (
<>
<ClassChild text={text} />
<FunctionChild text={text} />
</>
);
}

let renderer;
await act(() => {
renderer = ReactTestRenderer.create(<App text={'mount'} />, {
unstable_isConcurrent: true,
});
});

if (supportsDoubleInvokeEffects()) {
assertLog([
'useLayoutEffect mount',
'useEffect mount',
'componentWillUnmount',
'useLayoutEffect unmount',
'useEffect unmount',
'useLayoutEffect mount',
'useEffect mount',
]);
} else {
assertLog(['useLayoutEffect mount', 'useEffect mount']);
}

await act(() => {
renderer.update(<App text={'mount'} />);
});

assertLog([
'useLayoutEffect unmount',
'useLayoutEffect mount',
'useEffect unmount',
'useEffect mount',
]);

await act(() => {
renderer.unmount();
});

assertLog([
'componentWillUnmount',
'useLayoutEffect unmount',
'useEffect unmount',
]);
});
});