Skip to content

Commit

Permalink
Updated stateless ref warning message with more info
Browse files Browse the repository at this point in the history
  • Loading branch information
bspaulding committed Sep 25, 2015
1 parent af79118 commit 63cfcca
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/renderers/shared/reconciler/ReactCompositeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,16 @@ var ReactCompositeComponentMixin = {
var inst = this.getPublicInstance();
invariant(inst != null, 'Stateless function components cannot have refs.');
var publicComponentInstance = component.getPublicInstance();
warning(publicComponentInstance != null, 'Stateless function components cannot be given refs.');
var componentName = component && component.getName ?
component.getName() : 'a component';
warning(publicComponentInstance != null,
'Stateless function components cannot be given refs ' +
'(See ref "%s" in %s created by %s). ' +
'Attempts to access this ref will fail.',
ref,
componentName,
this.getName()
);
var refs = inst.refs === emptyObject ? (inst.refs = {}) : inst.refs;
refs[ref] = publicComponentInstance;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ describe('ReactStatelessComponent', function() {
spyOn(console, 'error');

var Parent = React.createClass({
displayName: 'Parent',
render: function() {
return <StatelessComponent name="A" ref="stateless"/>;
},
Expand All @@ -137,7 +138,9 @@ describe('ReactStatelessComponent', function() {

expect(console.error.argsForCall.length).toBe(1);
expect(console.error.argsForCall[0][0]).toContain(
'Stateless function components cannot be given refs.'
'Stateless function components cannot be given refs ' +
'(See ref "stateless" in StatelessComponent created by Parent). ' +
'Attempts to access this ref will fail.'
);
});

Expand Down

0 comments on commit 63cfcca

Please sign in to comment.