Composite component throws on attaching ref to stateless component #4939

This commit is contained in:
Bradley Spaulding committed 2015-09-22 14:28:38 -07:00
1 parent 06568114f3
commit 650fa58ab9
2 files changed
+17 -1

No files matched your search

@@ -808,8 +808,10 @@ var ReactCompositeComponentMixin = {
attachRef: function(ref, component) {
var inst = this.getPublicInstance();
invariant(inst != null, 'Stateless function components cannot have refs.');
var componentInstance = component.getPublicInstance();
invariant(componentInstance != null, 'Stateless function components cannot be given refs.');
var refs = inst.refs === emptyObject ? (inst.refs = {}) : inst.refs;
refs[ref] = component.getPublicInstance();
refs[ref] = componentInstance;
},
/**
@@ -125,6 +125,20 @@ describe('ReactStatelessComponent', function() {
);
});
it('should throw when given a ref', function() {
var Parent = React.createClass({
render: function() {
return <StatelessComponent name="A" ref="stateless"/>;
},
});
expect(function() {
ReactTestUtils.renderIntoDocument(<Parent/>);
}).toThrow(
'Invariant Violation: Stateless function components cannot be given refs.'
);
});
it('should provide a null ref', function() {
function Child() {
return <div />;