From d98a3d92697eeb9d1d5dc3ffd7e4abf7fddf45c9 Mon Sep 17 00:00:00 2001 From: Andrew Clark Date: Tue, 21 Feb 2017 14:29:30 -0800 Subject: [PATCH] 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. --- .../ReactCompositeComponentState-test.js | 26 +++++++++++++++++++ .../shared/fiber/ReactFiberClassComponent.js | 4 +++ 2 files changed, 30 insertions(+) diff --git a/src/renderers/__tests__/ReactCompositeComponentState-test.js b/src/renderers/__tests__/ReactCompositeComponentState-test.js index 9018953438..2d9351da1e 100644 --- a/src/renderers/__tests__/ReactCompositeComponentState-test.js +++ b/src/renderers/__tests__/ReactCompositeComponentState-test.js @@ -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(, container); + // Update + ReactDOM.render(, container); + + expect(ops).toEqual(['step: 3, extra: false']); + }); }); diff --git a/src/renderers/shared/fiber/ReactFiberClassComponent.js b/src/renderers/shared/fiber/ReactFiberClassComponent.js index 87f3426099..13b6d78010 100644 --- a/src/renderers/shared/fiber/ReactFiberClassComponent.js +++ b/src/renderers/shared/fiber/ReactFiberClassComponent.js @@ -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); + } } }