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 [8110222](8110222)
  • Loading branch information
tyao1 committed Jun 1, 2023
1 parent 9908c7d commit d6f9de5
Show file tree
Hide file tree
Showing 19 changed files with 1,658 additions and 1,706 deletions.
2 changes: 1 addition & 1 deletion compiled/facebook-www/REVISION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
018c58c9c65452cff25aaf1f38f78a9b90d8e5c1
811022232efed62a3c943701bc99d18655bc78b3
2 changes: 1 addition & 1 deletion compiled/facebook-www/React-dev.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ if (
}
"use strict";

var ReactVersion = "18.3.0-www-modern-d087d75f";
var ReactVersion = "18.3.0-www-modern-a6d62651";

// ATTENTION
// When adding new symbols to this file,
Expand Down
2 changes: 1 addition & 1 deletion compiled/facebook-www/React-prod.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,4 +641,4 @@ exports.useSyncExternalStore = function (
);
};
exports.useTransition = useTransition;
exports.version = "18.3.0-www-modern-fb2ee8b7";
exports.version = "18.3.0-www-modern-916dbaab";
52 changes: 23 additions & 29 deletions compiled/facebook-www/ReactART-dev.classic.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function _assertThisInitialized(self) {
return self;
}

var ReactVersion = "18.3.0-www-classic-603b8e54";
var ReactVersion = "18.3.0-www-classic-3fed7141";

var LegacyRoot = 0;
var ConcurrentRoot = 1;
Expand Down Expand Up @@ -12590,13 +12590,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 @@ -12658,13 +12656,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 @@ -12710,25 +12706,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 @@ -23789,10 +23781,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
52 changes: 23 additions & 29 deletions compiled/facebook-www/ReactART-dev.modern.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function _assertThisInitialized(self) {
return self;
}

var ReactVersion = "18.3.0-www-modern-d087d75f";
var ReactVersion = "18.3.0-www-modern-a6d62651";

var LegacyRoot = 0;
var ConcurrentRoot = 1;
Expand Down Expand Up @@ -12322,13 +12322,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 @@ -12383,13 +12381,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 @@ -12435,25 +12431,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 @@ -23454,10 +23446,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
Loading

0 comments on commit d6f9de5

Please sign in to comment.