From 263800e09ba778769c36757f6187bc336cbf9ce1 Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Wed, 4 Feb 2015 11:50:04 -0800 Subject: [PATCH] Fix isMounted inside of render This is apparently used to determine if you can access refs. Bad pattern, but a pattern nonetheless. --- src/core/ReactCompositeComponent.js | 29 ++++++++++--------- src/core/__tests__/ReactComponent-test.js | 1 + .../__tests__/ReactComponentLifeCycle-test.js | 14 ++++++++- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/core/ReactCompositeComponent.js b/src/core/ReactCompositeComponent.js index e5e1998125..1382b54a74 100644 --- a/src/core/ReactCompositeComponent.js +++ b/src/core/ReactCompositeComponent.js @@ -196,22 +196,25 @@ var ReactCompositeComponentMixin = { this._pendingReplaceState = false; this._pendingForceUpdate = false; - if (inst.componentWillMount) { - var previouslyMounting = ReactLifeCycle.currentlyMountingInstance; - ReactLifeCycle.currentlyMountingInstance = this; - try { - inst.componentWillMount(); - } finally { - ReactLifeCycle.currentlyMountingInstance = previouslyMounting; - } - // When mounting, calls to `setState` by `componentWillMount` will set - // `this._pendingStateQueue` without triggering a re-render. - if (this._pendingStateQueue) { - inst.state = this._processPendingState(inst.props, inst.context); + var renderedElement; + + var previouslyMounting = ReactLifeCycle.currentlyMountingInstance; + ReactLifeCycle.currentlyMountingInstance = this; + try { + if (inst.componentWillMount) { + inst.componentWillMount(); + // When mounting, calls to `setState` by `componentWillMount` will set + // `this._pendingStateQueue` without triggering a re-render. + if (this._pendingStateQueue) { + inst.state = this._processPendingState(inst.props, inst.context); + } } + + renderedElement = this._renderValidatedComponent(); + } finally { + ReactLifeCycle.currentlyMountingInstance = previouslyMounting; } - var renderedElement = this._renderValidatedComponent(); this._renderedComponent = this._instantiateReactComponent( renderedElement, this._currentElement.type // The wrapping type diff --git a/src/core/__tests__/ReactComponent-test.js b/src/core/__tests__/ReactComponent-test.js index c09376a27f..2a2be2a974 100644 --- a/src/core/__tests__/ReactComponent-test.js +++ b/src/core/__tests__/ReactComponent-test.js @@ -263,6 +263,7 @@ describe('ReactComponent', function() { expect(this.isMounted()).toBeTruthy(); }, render: function() { + expect(this.isMounted()).toBeFalsy() return
; } }); diff --git a/src/core/__tests__/ReactComponentLifeCycle-test.js b/src/core/__tests__/ReactComponentLifeCycle-test.js index 2ad8a45837..04dd9003e3 100644 --- a/src/core/__tests__/ReactComponentLifeCycle-test.js +++ b/src/core/__tests__/ReactComponentLifeCycle-test.js @@ -260,6 +260,18 @@ describe('ReactComponentLifeCycle', function() { ); }); + it('is not mounted inside initial render', function() { + var InitialRender = React.createClass({ + render: function() { + expect(this.isMounted()).toBe(false); + return ( +
+ ); + } + }); + ReactTestUtils.renderIntoDocument(); + }); + it('should carry through each of the phases of setup', function() { var LifeCycleComponent = React.createClass({ getInitialState: function() { @@ -360,7 +372,7 @@ describe('ReactComponentLifeCycle', function() { ComponentLifeCycle.MOUNTED ); expect(instance._testJournal.compositeLifeCycleInInitialRender).toBe( - null + CompositeComponentLifeCycle.MOUNTING ); expect(getLifeCycleState(instance)).toBe(ComponentLifeCycle.MOUNTED);