Cleanup ReactCurrentOwner on Fatal

If a React component's render() fatals, it may contaminate
ReactCurrentOwner. This will cause the owner to be set improperly for
the next React.renderComponent() invocation (which causes an owner to be
set when there shouldn't be one).
This commit is contained in:
CommitSyncScript
2013-06-06 14:29:44 -07:00
committed by Paul O’Shannessy
parent 11a7cb5b73
commit a06de4bc4f
2 changed files with 28 additions and 2 deletions
+9 -2
View File
@@ -734,9 +734,16 @@ var ReactCompositeComponentMixin = {
* @private
*/
_renderValidatedComponent: function() {
var renderedComponent;
ReactCurrentOwner.current = this;
var renderedComponent = this.render();
ReactCurrentOwner.current = null;
try {
renderedComponent = this.render();
} catch (error) {
// IE8 requires `catch` in order to use `finally`.
throw error;
} finally {
ReactCurrentOwner.current = null;
}
invariant(
ReactComponent.isValidComponent(renderedComponent),
'%s.render(): A valid ReactComponent must be returned.',
@@ -23,6 +23,7 @@ var MorphingComponent;
var MorphingAutoBindComponent;
var ChildUpdates;
var React;
var ReactCurrentOwner;
var ReactProps;
var ReactTestUtils;
@@ -35,6 +36,7 @@ describe('ReactCompositeComponent', function() {
cx = require('cx');
reactComponentExpect = require('reactComponentExpect');
React = require('React');
ReactCurrentOwner = require('ReactCurrentOwner');
ReactProps = require('ReactProps');
ReactTestUtils = require('ReactTestUtils');
@@ -302,4 +304,21 @@ describe('ReactCompositeComponent', function() {
);
});
it('should cleanup even if render() fatals', function() {
var BadComponent = React.createClass({
render: function() {
throw new Error();
}
});
var instance = <BadComponent />;
expect(ReactCurrentOwner.current).toBe(null);
expect(function() {
ReactTestUtils.renderIntoDocument(instance);
}).toThrow();
expect(ReactCurrentOwner.current).toBe(null);
});
});