Skip to content

Commit

Permalink
Always trigger componentWillUnmount in StrictMode (#26842)
Browse files Browse the repository at this point in the history
<!--
  Thanks for submitting a pull request!
We appreciate you spending the time to work on these changes. Please
provide enough information so that others can review your pull request.
The three fields below are mandatory.

Before submitting a pull request, please make sure the following is
done:

1. Fork [the repository](https://github.com/facebook/react) and create
your branch from `main`.
  2. Run `yarn` in the repository root.
3. If you've fixed a bug or added code that should be tested, add tests!
4. Ensure the test suite passes (`yarn test`). Tip: `yarn test --watch
TestName` is helpful in development.
5. Run `yarn test --prod` to test in the production environment. It
supports the same options as `yarn test`.
6. If you need a debugger, run `yarn test --debug --watch TestName`,
open `chrome://inspect`, and press "Inspect".
7. Format your code with
[prettier](https://github.com/prettier/prettier) (`yarn prettier`).
8. Make sure your code lints (`yarn lint`). Tip: `yarn linc` to only
check changed files.
  9. Run the [Flow](https://flowtype.org/) type checks (`yarn flow`).
  10. If you haven't already, complete the CLA.

Learn more about contributing:
https://reactjs.org/docs/how-to-contribute.html
-->

## Summary

<!--
Explain the **motivation** for making this change. What existing problem
does the pull request solve?
-->
In StrictMode, React currently only triggers `componentWillUnmount` if
`componentDidMount` is defined. This would miss detecting issues like
initializing resources in constructor or componentWillMount, for
example:
```
class Component {
  constructor() {
     this._subscriptions = new Subscriptions();
  }
  componentWillUnmount() {
     this._subscriptions.reset();
  }
}
```

The PR makes `componentWillUnmount` always run in StrictMode.

## How did you test this change?

<!--
Demonstrate the code is solid. Example: The exact commands you ran and
their output, screenshots / videos if the pull request changes the user
interface.
How exactly did you verify that your PR solves the issue you wanted to
solve?
  If you leave this empty, your PR will very likely be closed.
-->
`yarn test ci`

DiffTrain build for commit 8110222.
  • Loading branch information
tyao1 committed Jun 1, 2023
1 parent b80a98a commit 2c4d9ce
Show file tree
Hide file tree
Showing 13 changed files with 599 additions and 617 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @noflow
* @nolint
* @preventMunge
* @generated SignedSource<<e2649a21f4471fab672f885dc7d27434>>
* @generated SignedSource<<35eceb39002aa0301bede117a38f12c0>>
*/

'use strict';
Expand Down Expand Up @@ -10293,13 +10293,11 @@ function mountClassInstance(workInProgress, ctor, newProps, renderLanes) {
}

if (typeof instance.componentDidMount === "function") {
var fiberFlags = Update | LayoutStatic;

if ((workInProgress.mode & StrictEffectsMode) !== NoMode) {
fiberFlags |= MountLayoutDev;
}
workInProgress.flags |= Update | LayoutStatic;
}

workInProgress.flags |= fiberFlags;
if ((workInProgress.mode & StrictEffectsMode) !== NoMode) {
workInProgress.flags |= MountLayoutDev;
}
}

Expand Down Expand Up @@ -10361,13 +10359,11 @@ function resumeMountClassInstance(workInProgress, ctor, newProps, renderLanes) {
// 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") {
var fiberFlags = Update | LayoutStatic;

if ((workInProgress.mode & StrictEffectsMode) !== NoMode) {
fiberFlags |= MountLayoutDev;
}
workInProgress.flags |= Update | LayoutStatic;
}

workInProgress.flags |= fiberFlags;
if ((workInProgress.mode & StrictEffectsMode) !== NoMode) {
workInProgress.flags |= MountLayoutDev;
}

return false;
Expand Down Expand Up @@ -10413,25 +10409,21 @@ function resumeMountClassInstance(workInProgress, ctor, newProps, renderLanes) {
}

if (typeof instance.componentDidMount === "function") {
var _fiberFlags = Update | LayoutStatic;

if ((workInProgress.mode & StrictEffectsMode) !== NoMode) {
_fiberFlags |= MountLayoutDev;
}
workInProgress.flags |= Update | LayoutStatic;
}

workInProgress.flags |= _fiberFlags;
if ((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") {
var _fiberFlags2 = Update | LayoutStatic;

if ((workInProgress.mode & StrictEffectsMode) !== NoMode) {
_fiberFlags2 |= MountLayoutDev;
}
workInProgress.flags |= Update | LayoutStatic;
}

workInProgress.flags |= _fiberFlags2;
if ((workInProgress.mode & StrictEffectsMode) !== NoMode) {
workInProgress.flags |= MountLayoutDev;
} // If shouldComponentUpdate returned false, we should still update the
// memoized state to indicate that this work can be reused.

Expand Down Expand Up @@ -19783,10 +19775,12 @@ function invokeLayoutEffectMountInDEV(fiber) {
case ClassComponent: {
var instance = fiber.stateNode;

try {
instance.componentDidMount();
} catch (error) {
captureCommitPhaseError(fiber, fiber.return, error);
if (typeof instance.componentDidMount === "function") {
try {
instance.componentDidMount();
} catch (error) {
captureCommitPhaseError(fiber, fiber.return, error);
}
}

break;
Expand Down Expand Up @@ -23928,7 +23922,7 @@ function createFiberRoot(
return root;
}

var ReactVersion = "18.3.0-canary-018c58c9c-20230601";
var ReactVersion = "18.3.0-canary-811022232-20230601";

// Might add PROFILE later.

Expand Down
Loading

0 comments on commit 2c4d9ce

Please sign in to comment.