Skip to content

Commit

Permalink
consistent owner for stateless component (#6534)
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminghe authored and sophiebits committed May 11, 2016
1 parent cf15788 commit b11540c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
13 changes: 9 additions & 4 deletions src/renderers/shared/stack/reconciler/ReactCompositeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/renderers/shared/stack/reconciler/ReactRef.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
};

Expand Down
34 changes: 34 additions & 0 deletions src/renderers/shared/stack/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 b11540c

Please sign in to comment.