@samitouri / QOS-React-2 / commits / 55f5cdee01

Disable setState before mount in legacy mode (#18851)

We kind of "support" this pattern in legacy mode. It's only deprecated in Concurrent Mode.

Sebastian Markbåge committed May 6, 2020 at 20:30 UTC 55f5cdee01ed25548ab55f2902a295df4324260d
3 files changed +37
packages/react-dom/src/__tests__/ReactCompositeComponentState-test.js
+29
@@ -510,4 +510,33 @@ describe('ReactCompositeComponent-state', () => {
510 expect(el.textContent).toBe('count:4');
511 });
512 }
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 + });
542 });
packages/react-reconciler/src/ReactFiberWorkLoop.new.js
+4
@@ -2760,6 +2760,10 @@ function warnAboutUpdateOnNotYetMountedFiberInDEV(fiber) {
2760 return;
2761 }
2762
2763 + if (!(fiber.mode & (BlockingMode | ConcurrentMode))) {
2764 + return;
2765 + }
2766 +
2767 const tag = fiber.tag;
2768 if (
2769 tag !== IndeterminateComponent &&
packages/react-reconciler/src/ReactFiberWorkLoop.old.js
+4
@@ -2927,6 +2927,10 @@ function warnAboutUpdateOnNotYetMountedFiberInDEV(fiber) {
2927 return;
2928 }
2929
2930 + if (!(fiber.mode & (BlockingMode | ConcurrentMode))) {
2931 + return;
2932 + }
2933 +
2934 const tag = fiber.tag;
2935 if (
2936 tag !== IndeterminateComponent &&