move lifecycle check into replaceProps instead of updateComponent

This commit is contained in:
Clay Allsopp
2013-09-01 18:42:01 -07:00
parent 88faef3ba9
commit 15f84a391d
3 changed files with 24 additions and 5 deletions
+4
View File
@@ -278,6 +278,10 @@ var ReactComponent = {
'`render` method to pass the correct value as props to the component ' +
'where it is created.'
);
invariant(
this.isMounted(),
'replaceProps(...): Can only update a mounted component.'
);
this._pendingProps = props;
ReactUpdates.enqueueUpdate(this, callback);
},
-5
View File
@@ -726,11 +726,6 @@ var ReactCompositeComponentMixin = {
ReactComponent.Mixin.updateComponent.call(this, transaction, prevProps);
var currentComponent = this._renderedComponent;
var nextComponent = this._renderValidatedComponent();
invariant(
typeof currentComponent !== 'undefined',
'updateComponent(...): You are attempting to update an unrendered ' +
'component.'
);
if (currentComponent.constructor === nextComponent.constructor) {
currentComponent.receiveProps(nextComponent.props, transaction);
} else {
@@ -408,6 +408,26 @@ describe('ReactComponentLifeCycle', function() {
.toThrow();
});
it('should throw when calling setProps() on an unmounted component', function() {
var PropsToUpdate = React.createClass({
render: function() {
return (
<input
value={this.props.value}
ref="theSimpleComponent">
</input>
);
}
});
var instance =
<PropsToUpdate
value="hello"
/>;
expect(function() {
instance.setProps({value: "goodbye"});
}).toThrow();
});
it('should allow state updates in componentDidMount', function() {
/**
* calls setState in an componentDidMount.