From b2bf83ec854d697c4e50c738538fb49f32a3eb38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20O=E2=80=99Shannessy?= Date: Tue, 23 Dec 2014 15:33:53 -0800 Subject: [PATCH] Temporarily fix EmptyComponents This a workaround for the problem described in #2770. It should be temporary because this is really just working around the real problem. --- src/core/ReactEmptyComponent.js | 11 ++++++ .../__tests__/ReactEmptyComponent-test.js | 34 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/core/ReactEmptyComponent.js b/src/core/ReactEmptyComponent.js index aed8169754..d40c28d79f 100644 --- a/src/core/ReactEmptyComponent.js +++ b/src/core/ReactEmptyComponent.js @@ -30,10 +30,21 @@ var ReactEmptyComponentInjection = { var ReactEmptyComponentType = function() {}; ReactEmptyComponentType.prototype.componentDidMount = function() { var internalInstance = ReactInstanceMap.get(this); + // TODO: Make sure we run these methods in the correct order, we shouldn't + // need this check. We're going to assume if we're here it means we ran + // componentWillUnmount already so there is no internal instance (it gets + // removed as part of the unmounting process). + if (!internalInstance) { + return; + } registerNullComponentID(internalInstance._rootNodeID); }; ReactEmptyComponentType.prototype.componentWillUnmount = function() { var internalInstance = ReactInstanceMap.get(this); + // TODO: Get rid of this check. See TODO in componentDidMount. + if (!internalInstance) { + return; + } deregisterNullComponentID(internalInstance._rootNodeID); }; ReactEmptyComponentType.prototype.render = function() { diff --git a/src/core/__tests__/ReactEmptyComponent-test.js b/src/core/__tests__/ReactEmptyComponent-test.js index 9882de9e60..962cecdbca 100644 --- a/src/core/__tests__/ReactEmptyComponent-test.js +++ b/src/core/__tests__/ReactEmptyComponent-test.js @@ -231,4 +231,38 @@ describe('ReactEmptyComponent', function() { 'Invariant Violation: React.render(): Invalid component element.' ); }); + + it('does not break when updating during mount', function() { + var Child = React.createClass({ + componentDidMount() { + this.props.onMount && this.props.onMount(); + }, + render() { + if (!this.props.visible) { + return null; + } + + return
hello world
; + } + }); + + var Parent = React.createClass({ + update() { + this.forceUpdate(); + }, + render() { + return ( +
+ + + +
+ ); + } + }); + + expect(function() { + ReactTestUtils.renderIntoDocument() + }).not.toThrow(); + }); });