@samitouri / QOS-React / commits / d3cbbe3b36

Add deprecation warning for assigning to this.state inside cWRP

We'll remove support for this in a future release, though we'll likely still warn.

Andrew Clark committed Feb 21, 2017 at 14:42 UTC d3cbbe3b363e662fb46d97d6f5f87b4010395ba2
3 files changed +26
src/renderers/__tests__/ReactCompositeComponentState-test.js
+8
@@ -416,6 +416,8 @@ describe('ReactCompositeComponent-state', () => {
416 });
417
418 it('should treat assigning to this.state inside cWRP as a replaceState, with a warning', () => {
419 + spyOn(console, 'error');
420 +
421 let ops = [];
422 class Test extends React.Component {
423 state = { step: 1, extra: true };
@@ -439,5 +441,11 @@ describe('ReactCompositeComponent-state', () => {
441 ReactDOM.render(<Test />, container);
442
443 expect(ops).toEqual(['step: 3, extra: false']);
444 + expect(console.error.calls.count()).toEqual(1);
445 + expect(console.error.calls.argsFor(0)[0]).toEqual(
446 + 'Warning: Test.componentWillReceiveProps(): Assigning directly to ' +
447 + 'this.state is deprecated (except inside a component\'s constructor). ' +
448 + 'Use setState instead.'
449 + );
450 });
451 });
src/renderers/shared/fiber/ReactFiberClassComponent.js
+9
@@ -410,6 +410,15 @@ module.exports = function(
410 instance.componentWillReceiveProps(newProps, newContext);
411
412 if (instance.state !== workInProgress.memoizedState) {
413 + if (__DEV__) {
414 + warning(
415 + false,
416 + '%s.componentWillReceiveProps(): Assigning directly to ' +
417 + 'this.state is deprecated (except inside a component\'s ' +
418 + 'constructor). Use setState instead.',
419 + getComponentName(workInProgress)
420 + );
421 + }
422 updater.enqueueReplaceState(instance, instance.state, null);
423 }
424 }
src/renderers/shared/stack/reconciler/ReactCompositeComponent.js
+9
@@ -864,6 +864,15 @@ var ReactCompositeComponent = {
864 if (beforeState !== afterState) {
865 inst.state = beforeState;
866 inst.updater.enqueueReplaceState(inst, afterState);
867 + if (__DEV__) {
868 + warning(
869 + false,
870 + '%s.componentWillReceiveProps(): Assigning directly to ' +
871 + 'this.state is deprecated (except inside a component\'s ' +
872 + 'constructor). Use setState instead.',
873 + this.getName() || 'ReactCompositeComponent'
874 + );
875 + }
876 }
877 }
878