Merge pull request #3039 from sebmarkbage/fixismounted

Fix isMounted inside of render
This commit is contained in:
Sebastian Markbåge
2015-02-04 11:53:07 -08:00
3 changed files with 30 additions and 14 deletions
+16 -13
View File
@@ -196,22 +196,25 @@ var ReactCompositeComponentMixin = {
this._pendingReplaceState = false;
this._pendingForceUpdate = false;
if (inst.componentWillMount) {
var previouslyMounting = ReactLifeCycle.currentlyMountingInstance;
ReactLifeCycle.currentlyMountingInstance = this;
try {
inst.componentWillMount();
} finally {
ReactLifeCycle.currentlyMountingInstance = previouslyMounting;
}
// When mounting, calls to `setState` by `componentWillMount` will set
// `this._pendingStateQueue` without triggering a re-render.
if (this._pendingStateQueue) {
inst.state = this._processPendingState(inst.props, inst.context);
var renderedElement;
var previouslyMounting = ReactLifeCycle.currentlyMountingInstance;
ReactLifeCycle.currentlyMountingInstance = this;
try {
if (inst.componentWillMount) {
inst.componentWillMount();
// When mounting, calls to `setState` by `componentWillMount` will set
// `this._pendingStateQueue` without triggering a re-render.
if (this._pendingStateQueue) {
inst.state = this._processPendingState(inst.props, inst.context);
}
}
renderedElement = this._renderValidatedComponent();
} finally {
ReactLifeCycle.currentlyMountingInstance = previouslyMounting;
}
var renderedElement = this._renderValidatedComponent();
this._renderedComponent = this._instantiateReactComponent(
renderedElement,
this._currentElement.type // The wrapping type
@@ -263,6 +263,7 @@ describe('ReactComponent', function() {
expect(this.isMounted()).toBeTruthy();
},
render: function() {
expect(this.isMounted()).toBeFalsy()
return <div/>;
}
});
@@ -260,6 +260,18 @@ describe('ReactComponentLifeCycle', function() {
);
});
it('is not mounted inside initial render', function() {
var InitialRender = React.createClass({
render: function() {
expect(this.isMounted()).toBe(false);
return (
<div></div>
);
}
});
ReactTestUtils.renderIntoDocument(<InitialRender />);
});
it('should carry through each of the phases of setup', function() {
var LifeCycleComponent = React.createClass({
getInitialState: function() {
@@ -360,7 +372,7 @@ describe('ReactComponentLifeCycle', function() {
ComponentLifeCycle.MOUNTED
);
expect(instance._testJournal.compositeLifeCycleInInitialRender).toBe(
null
CompositeComponentLifeCycle.MOUNTING
);
expect(getLifeCycleState(instance)).toBe(ComponentLifeCycle.MOUNTED);