@samitouri / QOS-React-2 / commits / 650fa58ab9

Composite component throws on attaching ref to stateless component #4939

Bradley Spaulding committed Sep 22, 2015 at 14:27 UTC 650fa58ab9990be74596899a0cb8212d6da3863f
2 files changed +17 -1
src/renderers/shared/reconciler/ReactCompositeComponent.js
+3 -1
@@ -808,8 +808,10 @@ var ReactCompositeComponentMixin = {
808 attachRef: function(ref, component) {
809 var inst = this.getPublicInstance();
810 invariant(inst != null, 'Stateless function components cannot have refs.');
811 + var componentInstance = component.getPublicInstance();
812 + invariant(componentInstance != null, 'Stateless function components cannot be given refs.');
813 var refs = inst.refs === emptyObject ? (inst.refs = {}) : inst.refs;
812 - refs[ref] = component.getPublicInstance();
814 + refs[ref] = componentInstance;
815 },
816
817 /**
src/renderers/shared/reconciler/__tests__/ReactStatelessComponent-test.js
+14
@@ -125,6 +125,20 @@ describe('ReactStatelessComponent', function() {
125 );
126 });
127
128 + it('should throw when given a ref', function() {
129 + var Parent = React.createClass({
130 + render: function() {
131 + return <StatelessComponent name="A" ref="stateless"/>;
132 + },
133 + });
134 +
135 + expect(function() {
136 + ReactTestUtils.renderIntoDocument(<Parent/>);
137 + }).toThrow(
138 + 'Invariant Violation: Stateless function components cannot be given refs.'
139 + );
140 + });
141 +
142 it('should provide a null ref', function() {
143 function Child() {
144 return <div />;