Skip to content

Commit

Permalink
Fix missing context to componentDidMount() when double-invoking lifec…
Browse files Browse the repository at this point in the history
…ycles
  • Loading branch information
Brian Vaughn committed Sep 30, 2020
1 parent 9198a5c commit 6010925
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 13 deletions.
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);
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);
}
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

0 comments on commit 6010925

Please sign in to comment.