Skip to content

Commit

Permalink
consistent owner for stateless component
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminghe committed Apr 19, 2016
1 parent dc6fc8c commit e437116
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/renderers/shared/reconciler/ReactCompositeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -900,12 +900,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
Expand Down
34 changes: 34 additions & 0 deletions src/renderers/shared/reconciler/__tests__/refs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});

0 comments on commit e437116

Please sign in to comment.