Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 55f5cde

Browse files
authored
Disable setState before mount in legacy mode (facebook#18851)
We kind of "support" this pattern in legacy mode. It's only deprecated in Concurrent Mode.
1 parent 47ebc90 commit 55f5cde

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

packages/react-dom/src/__tests__/ReactCompositeComponentState-test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,4 +510,33 @@ describe('ReactCompositeComponent-state', () => {
510510
expect(el.textContent).toBe('count:4');
511511
});
512512
}
513+
514+
it('should support setState in componentWillUnmount', () => {
515+
let subscription;
516+
class A extends React.Component {
517+
componentWillUnmount() {
518+
subscription();
519+
}
520+
render() {
521+
return 'A';
522+
}
523+
}
524+
525+
class B extends React.Component {
526+
state = {siblingUnmounted: false};
527+
UNSAFE_componentWillMount() {
528+
subscription = () => this.setState({siblingUnmounted: true});
529+
}
530+
render() {
531+
return 'B' + (this.state.siblingUnmounted ? ' No Sibling' : '');
532+
}
533+
}
534+
535+
const el = document.createElement('div');
536+
ReactDOM.render(<A />, el);
537+
expect(el.textContent).toBe('A');
538+
539+
ReactDOM.render(<B />, el);
540+
expect(el.textContent).toBe('B No Sibling');
541+
});
513542
});

packages/react-reconciler/src/ReactFiberWorkLoop.new.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2760,6 +2760,10 @@ function warnAboutUpdateOnNotYetMountedFiberInDEV(fiber) {
27602760
return;
27612761
}
27622762

2763+
if (!(fiber.mode & (BlockingMode | ConcurrentMode))) {
2764+
return;
2765+
}
2766+
27632767
const tag = fiber.tag;
27642768
if (
27652769
tag !== IndeterminateComponent &&

packages/react-reconciler/src/ReactFiberWorkLoop.old.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2927,6 +2927,10 @@ function warnAboutUpdateOnNotYetMountedFiberInDEV(fiber) {
29272927
return;
29282928
}
29292929

2930+
if (!(fiber.mode & (BlockingMode | ConcurrentMode))) {
2931+
return;
2932+
}
2933+
29302934
const tag = fiber.tag;
29312935
if (
29322936
tag !== IndeterminateComponent &&

0 commit comments

Comments
 (0)