mirror of
https://github.com/facebook/react.git
synced 2026-11-18 20:15:56 +00:00
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:
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'
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user