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

This commit is contained in:
Brian Vaughn 2020-09-30 15:56:19 -04:00 committed by GitHub
parent 9198a5cec0
commit 7f08e908b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 13 deletions

View File

@ -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);
if (hasCaughtError()) {
const mountError = clearCaughtError();
captureCommitPhaseError(fiber, fiber.return, mountError);
@ -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);
}
break;
}

View File

@ -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() {