@samitouri / QOS-React-2 / commits / af79118148

attachRef to a stateless component warns instead of throwing #4939

Bradley Spaulding committed Sep 24, 2015 at 11:52 UTC af7911814835891522d0d910428eb85e005ec92b
2 files changed +8 -6
src/renderers/shared/reconciler/ReactCompositeComponent.js
+1 -1
@@ -809,7 +809,7 @@ var ReactCompositeComponentMixin = {
809 var inst = this.getPublicInstance();
810 invariant(inst != null, 'Stateless function components cannot have refs.');
811 var publicComponentInstance = component.getPublicInstance();
812 - invariant(publicComponentInstance != null, 'Stateless function components cannot be given refs.');
812 + warning(publicComponentInstance != null, 'Stateless function components cannot be given refs.');
813 var refs = inst.refs === emptyObject ? (inst.refs = {}) : inst.refs;
814 refs[ref] = publicComponentInstance;
815 },
src/renderers/shared/reconciler/__tests__/ReactStatelessComponent-test.js
+7 -5
@@ -125,17 +125,19 @@ describe('ReactStatelessComponent', function() {
125 );
126 });
127
128 - it('should throw when given a ref', function() {
128 + it('should warn when given a ref', function() {
129 + spyOn(console, 'error');
130 +
131 var Parent = React.createClass({
132 render: function() {
133 return <StatelessComponent name="A" ref="stateless"/>;
134 },
135 });
136 + ReactTestUtils.renderIntoDocument(<Parent/>);
137
135 - expect(function() {
136 - ReactTestUtils.renderIntoDocument(<Parent/>);
137 - }).toThrow(
138 - 'Invariant Violation: Stateless function components cannot be given refs.'
138 + expect(console.error.argsForCall.length).toBe(1);
139 + expect(console.error.argsForCall[0][0]).toContain(
140 + 'Stateless function components cannot be given refs.'
141 );
142 });
143