Support assigning directly to this.state inside cWRP

We don't explicitly support this but it happens to work in Stack. We'll
give it the same semantics as replaceState.
This commit is contained in:
Andrew Clark committed 2017-02-21 14:29:30 -08:00
1 parent 51733c9a36
commit d98a3d9269
2 files changed
+30

No files matched your search

@@ -414,4 +414,30 @@ describe('ReactCompositeComponent-state', () => {
'scu from a,b to a,b,c',
]);
});
it('should treat assigning to this.state inside cWRP as a replaceState, with a warning', () => {
let ops = [];
class Test extends React.Component {
state = { step: 1, extra: true };
componentWillReceiveProps() {
this.setState({ step: 2 }, () => {
// Tests that earlier setState callbacks are not dropped
ops.push(`step: ${this.state.step}, extra: ${!!this.state.extra}`);
});
// Treat like replaceState
this.state = { step: 3 };
}
render() {
return null;
}
}
// Mount
const container = document.createElement('div');
ReactDOM.render(<Test />, container);
// Update
ReactDOM.render(<Test />, container);
expect(ops).toEqual(['step: 3, extra: false']);
});
});
@@ -408,6 +408,10 @@ module.exports = function(
if (oldProps !== newProps || oldContext !== newContext) {
if (typeof instance.componentWillReceiveProps === 'function') {
instance.componentWillReceiveProps(newProps, newContext);
if (instance.state !== workInProgress.memoizedState) {
updater.enqueueReplaceState(instance, instance.state, null);
}
}
}