consistent owner for stateless component (#6534)

This commit is contained in:
yiminghe
2016-05-10 22:21:16 -07:00
committed by Ben Alpert
parent cf157886e9
commit b11540ccb2
3 changed files with 47 additions and 6 deletions
@@ -1117,12 +1117,17 @@ var ReactCompositeComponentMixin = {
*/
_renderValidatedComponent: function() {
var renderedComponent;
ReactCurrentOwner.current = this;
try {
if (__DEV__ || !(this._instance instanceof StatelessComponent)) {
ReactCurrentOwner.current = this;
try {
renderedComponent =
this._renderValidatedComponentWithoutOwnerOrContext();
} finally {
ReactCurrentOwner.current = null;
}
} else {
renderedComponent =
this._renderValidatedComponentWithoutOwnerOrContext();
} finally {
ReactCurrentOwner.current = null;
}
invariant(
// TODO: An `isValidNode` function would probably be more appropriate
@@ -62,8 +62,10 @@ ReactRef.shouldUpdateRefs = function(prevElement, nextElement) {
return (
// This has a few false positives w/r/t empty components.
prevEmpty || nextEmpty ||
nextElement._owner !== prevElement._owner ||
nextElement.ref !== prevElement.ref
nextElement.ref !== prevElement.ref ||
// If owner changes but we have an unchanged function ref, don't update refs
(typeof nextElement.ref === 'string' &&
nextElement._owner !== prevElement._owner)
);
};
@@ -240,5 +240,39 @@ describe('ref swapping', function() {
var instance = ReactTestUtils.renderIntoDocument(<Component />);
expect(!!instance.refs).toBe(true);
});
function testRefCall() {
var refCalled = 0;
function Inner(props) {
return <a ref={props.saveA} />;
}
var Outer = React.createClass({
saveA() {
refCalled++;
},
componentDidMount() {
this.setState({});
},
render() {
return <Inner saveA={this.saveA} />;
},
});
ReactTestUtils.renderIntoDocument(<Outer />);
expect(refCalled).toBe(1);
}
it('ref called correctly for stateless component when __DEV__ = false', function() {
var originalDev = __DEV__;
__DEV__ = false;
testRefCall();
__DEV__ = originalDev;
});
it('ref called correctly for stateless component when __DEV__ = true', function() {
var originalDev = __DEV__;
__DEV__ = true;
testRefCall();
__DEV__ = originalDev;
});
});