Updated stateless ref warning message with more info
Bradley Spaulding committed
Sep 25, 2015 at 16:21 UTC
63cfcca74eb9805720968e69b18c09f4c80e188d
2 files changed
+14
-2
src/renderers/shared/reconciler/ReactCompositeComponent.js
+10
-1
@@ -809,7 +809,16 @@ var ReactCompositeComponentMixin = {
809
var inst = this.getPublicInstance();
810
invariant(inst != null, 'Stateless function components cannot have refs.');
811
var publicComponentInstance = component.getPublicInstance();
812
- warning(publicComponentInstance != null, 'Stateless function components cannot be given refs.');
812
+ var componentName = component && component.getName ?
813
+ component.getName() : 'a component';
814
+ warning(publicComponentInstance != null,
815
+ 'Stateless function components cannot be given refs ' +
816
+ '(See ref "%s" in %s created by %s). ' +
817
+ 'Attempts to access this ref will fail.',
818
+ ref,
819
+ componentName,
820
+ this.getName()
821
+ );
822
var refs = inst.refs === emptyObject ? (inst.refs = {}) : inst.refs;
823
refs[ref] = publicComponentInstance;
824
},
src/renderers/shared/reconciler/__tests__/ReactStatelessComponent-test.js
+4
-1
@@ -129,6 +129,7 @@ describe('ReactStatelessComponent', function() {
129
spyOn(console, 'error');
130
131
var Parent = React.createClass({
132
+ displayName: 'Parent',
133
render: function() {
134
return <StatelessComponent name="A" ref="stateless"/>;
135
},
@@ -137,7 +138,9 @@ describe('ReactStatelessComponent', function() {
138
139
expect(console.error.argsForCall.length).toBe(1);
140
expect(console.error.argsForCall[0][0]).toContain(
140
- 'Stateless function components cannot be given refs.'
141
+ 'Stateless function components cannot be given refs ' +
142
+ '(See ref "stateless" in StatelessComponent created by Parent). ' +
143
+ 'Attempts to access this ref will fail.'
144
);
145
});
146