mirror of
https://github.com/zebrajr/react.git
synced 2025-12-06 12:20:20 +01:00
Fix missing context to componentDidMount() when double-invoking lifecycles (#19935)
This commit is contained in:
parent
9198a5cec0
commit
7f08e908b1
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user