Skip to content

Commit

Permalink
Respect state set in componentWillMount() on resuming (facebook#8079)
Browse files Browse the repository at this point in the history
When resuming creating new instance after resuming work, we should set the `state` on the newly creating instance rather than the old one.
  • Loading branch information
gaearon authored Oct 24, 2016
1 parent 9626c83 commit 265ab83
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/renderers/shared/fiber/ReactFiberClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ module.exports = function(scheduleUpdate : (fiber: Fiber, priorityLevel : Priori
// process them now.
const newUpdateQueue = workInProgress.updateQueue;
if (newUpdateQueue) {
instance.state = mergeUpdateQueue(newUpdateQueue, newState, newProps);
newInstance.state = mergeUpdateQueue(newUpdateQueue, newState, newProps);
}
}
return true;
Expand Down
47 changes: 47 additions & 0 deletions src/renderers/shared/fiber/__tests__/ReactIncremental-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,53 @@ describe('ReactIncremental', () => {
]);
});

it('uses state set in componentWillMount even if initial render was aborted', () => {
var ops = [];

class LifeCycle extends React.Component {
constructor(props) {
super(props);
this.state = {x: this.props.x + '(ctor)'};
}
componentWillMount() {
ops.push('componentWillMount:' + this.state.x);
this.setState({x: this.props.x + '(willMount)'});
}
componentDidMount() {
ops.push('componentDidMount:' + this.state.x);
}
render() {
ops.push('render:' + this.state.x);
return <span />;
}
}

function App(props) {
ops.push('App');
return <LifeCycle x={props.x} />;
}

ReactNoop.render(<App x={0} />);
ReactNoop.flushDeferredPri(20);

expect(ops).toEqual([
'App',
'componentWillMount:0(ctor)',
'render:0(willMount)',
]);

ops = [];
ReactNoop.render(<App x={1} />);
ReactNoop.flush();

expect(ops).toEqual([
'App',
'componentWillMount:1(ctor)',
'render:1(willMount)',
'componentDidMount:1(willMount)',
]);
});

it('calls componentWill* twice if an update render is aborted', () => {
var ops = [];

Expand Down

0 comments on commit 265ab83

Please sign in to comment.