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.
This commit is contained in:
Andrew Clark committed 2017-02-21 14:42:12 -08:00
1 parent adc1641fec
commit d3cbbe3b36
3 files changed
+26

No files matched your search

@@ -416,6 +416,8 @@ describe('ReactCompositeComponent-state', () => {
});
it('should treat assigning to this.state inside cWRP as a replaceState, with a warning', () => {
spyOn(console, 'error');
let ops = [];
class Test extends React.Component {
state = { step: 1, extra: true };
@@ -439,5 +441,11 @@ describe('ReactCompositeComponent-state', () => {
ReactDOM.render(<Test />, container);
expect(ops).toEqual(['step: 3, extra: false']);
expect(console.error.calls.count()).toEqual(1);
expect(console.error.calls.argsFor(0)[0]).toEqual(
'Warning: Test.componentWillReceiveProps(): Assigning directly to ' +
'this.state is deprecated (except inside a component\'s constructor). ' +
'Use setState instead.'
);
});
});
@@ -410,6 +410,15 @@ module.exports = function(
instance.componentWillReceiveProps(newProps, newContext);
if (instance.state !== workInProgress.memoizedState) {
if (__DEV__) {
warning(
false,
'%s.componentWillReceiveProps(): Assigning directly to ' +
'this.state is deprecated (except inside a component\'s ' +
'constructor). Use setState instead.',
getComponentName(workInProgress)
);
}
updater.enqueueReplaceState(instance, instance.state, null);
}
}
@@ -864,6 +864,15 @@ var ReactCompositeComponent = {
if (beforeState !== afterState) {
inst.state = beforeState;
inst.updater.enqueueReplaceState(inst, afterState);
if (__DEV__) {
warning(
false,
'%s.componentWillReceiveProps(): Assigning directly to ' +
'this.state is deprecated (except inside a component\'s ' +
'constructor). Use setState instead.',
this.getName() || 'ReactCompositeComponent'
);
}
}
}