From 650fa58ab9990be74596899a0cb8212d6da3863f Mon Sep 17 00:00:00 2001 From: Bradley Spaulding Date: Tue, 22 Sep 2015 14:27:17 -0700 Subject: [PATCH] Composite component throws on attaching ref to stateless component #4939 --- .../shared/reconciler/ReactCompositeComponent.js | 4 +++- .../__tests__/ReactStatelessComponent-test.js | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/renderers/shared/reconciler/ReactCompositeComponent.js b/src/renderers/shared/reconciler/ReactCompositeComponent.js index b96ece5133..c4729e39ea 100644 --- a/src/renderers/shared/reconciler/ReactCompositeComponent.js +++ b/src/renderers/shared/reconciler/ReactCompositeComponent.js @@ -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; }, /** diff --git a/src/renderers/shared/reconciler/__tests__/ReactStatelessComponent-test.js b/src/renderers/shared/reconciler/__tests__/ReactStatelessComponent-test.js index c94c60f827..4c07358a2a 100644 --- a/src/renderers/shared/reconciler/__tests__/ReactStatelessComponent-test.js +++ b/src/renderers/shared/reconciler/__tests__/ReactStatelessComponent-test.js @@ -125,6 +125,20 @@ describe('ReactStatelessComponent', function() { ); }); + it('should throw when given a ref', function() { + var Parent = React.createClass({ + render: function() { + return ; + }, + }); + + expect(function() { + ReactTestUtils.renderIntoDocument(); + }).toThrow( + 'Invariant Violation: Stateless function components cannot be given refs.' + ); + }); + it('should provide a null ref', function() { function Child() { return
;