@samitouri / QOS-React-2 / commits / 15f84a391d

move lifecycle check into replaceProps instead of updateComponent

Clay Allsopp committed Sep 1, 2013 at 18:38 UTC 15f84a391d9506774db069bccb1f89ebf8987d1a
3 files changed +24 -5
src/core/ReactComponent.js
+4
@@ -278,6 +278,10 @@ var ReactComponent = {
278 '`render` method to pass the correct value as props to the component ' +
279 'where it is created.'
280 );
281 + invariant(
282 + this.isMounted(),
283 + 'replaceProps(...): Can only update a mounted component.'
284 + );
285 this._pendingProps = props;
286 ReactUpdates.enqueueUpdate(this, callback);
287 },
src/core/ReactCompositeComponent.js
-5
@@ -726,11 +726,6 @@ var ReactCompositeComponentMixin = {
726 ReactComponent.Mixin.updateComponent.call(this, transaction, prevProps);
727 var currentComponent = this._renderedComponent;
728 var nextComponent = this._renderValidatedComponent();
729 - invariant(
730 - typeof currentComponent !== 'undefined',
731 - 'updateComponent(...): You are attempting to update an unrendered ' +
732 - 'component.'
733 - );
729 if (currentComponent.constructor === nextComponent.constructor) {
730 currentComponent.receiveProps(nextComponent.props, transaction);
731 } else {
src/core/__tests__/ReactComponentLifeCycle-test.js
+20
@@ -408,6 +408,26 @@ describe('ReactComponentLifeCycle', function() {
408 .toThrow();
409 });
410
411 + it('should throw when calling setProps() on an unmounted component', function() {
412 + var PropsToUpdate = React.createClass({
413 + render: function() {
414 + return (
415 + <input
416 + value={this.props.value}
417 + ref="theSimpleComponent">
418 + </input>
419 + );
420 + }
421 + });
422 + var instance =
423 + <PropsToUpdate
424 + value="hello"
425 + />;
426 + expect(function() {
427 + instance.setProps({value: "goodbye"});
428 + }).toThrow();
429 + });
430 +
431 it('should allow state updates in componentDidMount', function() {
432 /**
433 * calls setState in an componentDidMount.