Should be able to setState inside cWRP before assigning to this.state

...without dropping the update. This won't work the other way around,
if you assign to this.state before calling setState. we'll add a
deprecation warning so people stop relying on this pattern.
This commit is contained in:
Andrew Clark 2017-02-21 14:40:56 -08:00
parent d98a3d9269
commit adc1641fec
1 changed files with 6 additions and 0 deletions

View File

@ -850,6 +850,7 @@ var ReactCompositeComponent = {
// _pendingStateQueue which will ensure that any state updates gets // _pendingStateQueue which will ensure that any state updates gets
// immediately reconciled instead of waiting for the next batch. // immediately reconciled instead of waiting for the next batch.
if (willReceive && inst.componentWillReceiveProps) { if (willReceive && inst.componentWillReceiveProps) {
const beforeState = inst.state;
if (__DEV__) { if (__DEV__) {
measureLifeCyclePerf( measureLifeCyclePerf(
() => inst.componentWillReceiveProps(nextProps, nextContext), () => inst.componentWillReceiveProps(nextProps, nextContext),
@ -859,6 +860,11 @@ var ReactCompositeComponent = {
} else { } else {
inst.componentWillReceiveProps(nextProps, nextContext); inst.componentWillReceiveProps(nextProps, nextContext);
} }
const afterState = inst.state;
if (beforeState !== afterState) {
inst.state = beforeState;
inst.updater.enqueueReplaceState(inst, afterState);
}
} }
// If updating happens to enqueue any new updates, we shouldn't execute new // If updating happens to enqueue any new updates, we shouldn't execute new