mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Fix isMounted() for composite components
This behavior seemed incorrect for composite components. - isComponentMounted() represents ReactComponent's lifecycle of mounting - isMounted() represents ReactCompositeComponent's lifecycle of isMounted() Therefore, ReactComponents no longer have isMounted(). I think this is fine since it was not supposed to be public anyway.
This commit is contained in:
committed by
Paul O’Shannessy
parent
4f2d8dfe72
commit
14102e8a48
@@ -326,8 +326,9 @@ function validateMethodOverride(proto, name) {
|
||||
function validateLifeCycleOnReplaceState(instance) {
|
||||
var compositeLifeCycleState = instance._compositeLifeCycleState;
|
||||
invariant(
|
||||
instance.isMounted(),
|
||||
'replaceState(...): Can only update a mounted component.'
|
||||
instance.isMounted() ||
|
||||
compositeLifeCycleState === CompositeLifeCycle.MOUNTING,
|
||||
'replaceState(...): Can only update a mounted or mounting component.'
|
||||
);
|
||||
invariant(
|
||||
compositeLifeCycleState !== CompositeLifeCycle.RECEIVING_STATE &&
|
||||
@@ -480,6 +481,17 @@ var ReactCompositeComponentMixin = {
|
||||
ReactCurrentOwner.getDepth() + 1 : 0;
|
||||
},
|
||||
|
||||
/**
|
||||
* Checks whether or not this composite component is mounted.
|
||||
* @return {boolean} True if mounted, false otherwise.
|
||||
* @protected
|
||||
* @final
|
||||
*/
|
||||
isMounted: function() {
|
||||
return ReactComponent.Mixin.isMounted.call(this) &&
|
||||
this._compositeLifeCycleState !== CompositeLifeCycle.MOUNTING;
|
||||
},
|
||||
|
||||
/**
|
||||
* Initializes the component, renders markup, and registers event listeners.
|
||||
*
|
||||
@@ -756,8 +768,10 @@ var ReactCompositeComponentMixin = {
|
||||
forceUpdate: function() {
|
||||
var compositeLifeCycleState = this._compositeLifeCycleState;
|
||||
invariant(
|
||||
this.isMounted(),
|
||||
'forceUpdate(...): Can only force an update on mounted components.'
|
||||
this.isMounted() ||
|
||||
compositeLifeCycleState === CompositeLifeCycle.MOUNTING,
|
||||
'forceUpdate(...): Can only force an update on mounted or mounting ' +
|
||||
'components.'
|
||||
);
|
||||
invariant(
|
||||
compositeLifeCycleState !== CompositeLifeCycle.RECEIVING_STATE &&
|
||||
|
||||
@@ -95,4 +95,23 @@ describe('ReactComponent', function() {
|
||||
ReactTestUtils.renderIntoDocument(instance);
|
||||
});
|
||||
|
||||
it('should correctly determine if a component is mounted', function() {
|
||||
var Component = React.createClass({
|
||||
componentWillMount: function() {
|
||||
expect(this.isMounted()).toBeFalsy();
|
||||
},
|
||||
componentDidMount: function() {
|
||||
expect(this.isMounted()).toBeTruthy();
|
||||
},
|
||||
render: function() {
|
||||
return <div/>;
|
||||
}
|
||||
});
|
||||
|
||||
var instance = <Component />;
|
||||
|
||||
expect(instance.isMounted()).toBeFalsy();
|
||||
ReactTestUtils.renderIntoDocument(instance);
|
||||
expect(instance.isMounted()).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -308,7 +308,7 @@ describe('ReactCompositeComponent', function() {
|
||||
instance.forceUpdate();
|
||||
}).toThrow(
|
||||
'Invariant Violation: forceUpdate(...): Can only force an update on ' +
|
||||
'mounted components.'
|
||||
'mounted or mounting components.'
|
||||
);
|
||||
|
||||
React.renderComponent(instance, container);
|
||||
@@ -321,7 +321,7 @@ describe('ReactCompositeComponent', function() {
|
||||
instance.forceUpdate();
|
||||
}).toThrow(
|
||||
'Invariant Violation: forceUpdate(...): Can only force an update on ' +
|
||||
'mounted components.'
|
||||
'mounted or mounting components.'
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user