From 88faef3ba9042ccd30ce5ab064a73ca6e3ae5c21 Mon Sep 17 00:00:00 2001 From: Clay Allsopp Date: Mon, 26 Aug 2013 10:42:53 -0700 Subject: [PATCH 1/3] Add more helpful invariant if you're updating an unrendered component --- src/core/ReactCompositeComponent.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/core/ReactCompositeComponent.js b/src/core/ReactCompositeComponent.js index 4bb6a4fedd..01c1ccbb5d 100644 --- a/src/core/ReactCompositeComponent.js +++ b/src/core/ReactCompositeComponent.js @@ -726,6 +726,11 @@ 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 { From 15f84a391d9506774db069bccb1f89ebf8987d1a Mon Sep 17 00:00:00 2001 From: Clay Allsopp Date: Sun, 1 Sep 2013 18:38:53 -0700 Subject: [PATCH 2/3] move lifecycle check into replaceProps instead of updateComponent --- src/core/ReactComponent.js | 4 ++++ src/core/ReactCompositeComponent.js | 5 ----- .../__tests__/ReactComponentLifeCycle-test.js | 20 +++++++++++++++++++ 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/core/ReactComponent.js b/src/core/ReactComponent.js index d8c883b6d4..2f048502e6 100644 --- a/src/core/ReactComponent.js +++ b/src/core/ReactComponent.js @@ -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); }, diff --git a/src/core/ReactCompositeComponent.js b/src/core/ReactCompositeComponent.js index 01c1ccbb5d..4bb6a4fedd 100644 --- a/src/core/ReactCompositeComponent.js +++ b/src/core/ReactCompositeComponent.js @@ -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 { diff --git a/src/core/__tests__/ReactComponentLifeCycle-test.js b/src/core/__tests__/ReactComponentLifeCycle-test.js index 3f2df5e82e..62aab8f14a 100644 --- a/src/core/__tests__/ReactComponentLifeCycle-test.js +++ b/src/core/__tests__/ReactComponentLifeCycle-test.js @@ -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 ( + + + ); + } + }); + var instance = + ; + expect(function() { + instance.setProps({value: "goodbye"}); + }).toThrow(); + }); + it('should allow state updates in componentDidMount', function() { /** * calls setState in an componentDidMount. From 9dd8ef4777957138ace4c9728d15283e2c2d85e8 Mon Sep 17 00:00:00 2001 From: Clay Allsopp Date: Wed, 4 Sep 2013 18:13:33 -0700 Subject: [PATCH 3/3] fix formatting and test for correct error --- src/core/__tests__/ReactComponentLifeCycle-test.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/core/__tests__/ReactComponentLifeCycle-test.js b/src/core/__tests__/ReactComponentLifeCycle-test.js index 62aab8f14a..d6bce0d267 100644 --- a/src/core/__tests__/ReactComponentLifeCycle-test.js +++ b/src/core/__tests__/ReactComponentLifeCycle-test.js @@ -408,7 +408,8 @@ describe('ReactComponentLifeCycle', function() { .toThrow(); }); - it('should throw when calling setProps() on an unmounted component', function() { + it('should throw when calling setProps() on an unmounted component', + function() { var PropsToUpdate = React.createClass({ render: function() { return ( @@ -419,13 +420,13 @@ describe('ReactComponentLifeCycle', function() { ); } }); - var instance = - ; + var instance = ; expect(function() { instance.setProps({value: "goodbye"}); - }).toThrow(); + }).toThrow( + 'Invariant Violation: replaceProps(...): Can only update a ' + + 'mounted component.' + ); }); it('should allow state updates in componentDidMount', function() {