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

Fix missing context to componentDidMount() when double-invoking lifecycles #19935

Merged
merged 1 commit into from
Sep 30, 2020
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
15 changes: 2 additions & 13 deletions packages/react-reconciler/src/ReactFiberCommitWork.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -2044,7 +2044,7 @@ function invokeLayoutEffectMountInDEV(fiber: Fiber): void {
}
case ClassComponent: {
const instance = fiber.stateNode;
invokeGuardedCallback(null, instance.componentDidMount, null);
invokeGuardedCallback(null, instance.componentDidMount, instance);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was the bugfix.

if (hasCaughtError()) {
const mountError = clearCaughtError();
captureCommitPhaseError(fiber, fiber.return, mountError);
Expand Down Expand Up @@ -2103,18 +2103,7 @@ function invokeLayoutEffectUnmountInDEV(fiber: Fiber): void {
case ClassComponent: {
const instance = fiber.stateNode;
if (typeof instance.componentWillUnmount === 'function') {
invokeGuardedCallback(
null,
safelyCallComponentWillUnmount,
null,
fiber,
instance,
fiber.return,
);
if (hasCaughtError()) {
const unmountError = clearCaughtError();
captureCommitPhaseError(fiber, fiber.return, unmountError);
}
safelyCallComponentWillUnmount(fiber, instance, fiber.return);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We didn't need to wrap safelyCallComponentWillUnmount with invokeGuardedCallback, because it already uses invokeGuardedCallback internally.

}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,45 @@ describe('ReactDoubleInvokeEvents', () => {
expect(Scheduler).toHaveYielded([]);
});

it('passes the right context to class component lifecycles', () => {
class App extends React.PureComponent {
test() {}

componentDidMount() {
this.test();
Scheduler.unstable_yieldValue('componentDidMount');
}

componentDidUpdate() {
this.test();
Scheduler.unstable_yieldValue('componentDidUpdate');
}

componentWillUnmount() {
this.test();
Scheduler.unstable_yieldValue('componentWillUnmount');
}

render() {
return null;
}
}

ReactNoop.act(() => {
ReactNoop.render(<App />);
});

if (__DEV__ && __VARIANT__) {
expect(Scheduler).toHaveYielded([
'componentDidMount',
'componentWillUnmount',
'componentDidMount',
]);
} else {
expect(Scheduler).toHaveYielded(['componentDidMount']);
}
});

it('double invoking works for class components', () => {
class App extends React.PureComponent {
componentDidMount() {
Expand Down