Respect state set in componentWillMount() on resuming (#8079)
When resuming creating new instance after resuming work, we should set the `state` on the newly creating instance rather than the old one.
Dan Abramov committed
Oct 25, 2016 at 00:21 UTC
265ab83667ccebbe2edc2abf3909be1ae42646ed
2 files changed
+48
-1
src/renderers/shared/fiber/ReactFiberClassComponent.js
+1
-1
@@ -152,7 +152,7 @@ module.exports = function(scheduleUpdate : (fiber: Fiber, priorityLevel : Priori
152
// process them now.
153
const newUpdateQueue = workInProgress.updateQueue;
154
if (newUpdateQueue) {
155
- instance.state = mergeUpdateQueue(newUpdateQueue, newState, newProps);
155
+ newInstance.state = mergeUpdateQueue(newUpdateQueue, newState, newProps);
156
}
157
}
158
return true;
src/renderers/shared/fiber/__tests__/ReactIncremental-test.js
+47
@@ -993,6 +993,53 @@ describe('ReactIncremental', () => {
993
]);
994
});
995
996
+ it('uses state set in componentWillMount even if initial render was aborted', () => {
997
+ var ops = [];
998
+
999
+ class LifeCycle extends React.Component {
1000
+ constructor(props) {
1001
+ super(props);
1002
+ this.state = {x: this.props.x + '(ctor)'};
1003
+ }
1004
+ componentWillMount() {
1005
+ ops.push('componentWillMount:' + this.state.x);
1006
+ this.setState({x: this.props.x + '(willMount)'});
1007
+ }
1008
+ componentDidMount() {
1009
+ ops.push('componentDidMount:' + this.state.x);
1010
+ }
1011
+ render() {
1012
+ ops.push('render:' + this.state.x);
1013
+ return <span />;
1014
+ }
1015
+ }
1016
+
1017
+ function App(props) {
1018
+ ops.push('App');
1019
+ return <LifeCycle x={props.x} />;
1020
+ }
1021
+
1022
+ ReactNoop.render(<App x={0} />);
1023
+ ReactNoop.flushDeferredPri(20);
1024
+
1025
+ expect(ops).toEqual([
1026
+ 'App',
1027
+ 'componentWillMount:0(ctor)',
1028
+ 'render:0(willMount)',
1029
+ ]);
1030
+
1031
+ ops = [];
1032
+ ReactNoop.render(<App x={1} />);
1033
+ ReactNoop.flush();
1034
+
1035
+ expect(ops).toEqual([
1036
+ 'App',
1037
+ 'componentWillMount:1(ctor)',
1038
+ 'render:1(willMount)',
1039
+ 'componentDidMount:1(willMount)',
1040
+ ]);
1041
+ });
1042
+
1043
it('calls componentWill* twice if an update render is aborted', () => {
1044
var ops = [];
1045