Skip to content

Commit

Permalink
Add component/owner name to warning
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Dail authored and Brandon Dail committed Nov 20, 2016
1 parent b6aaa92 commit 75f9efe
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
14 changes: 10 additions & 4 deletions src/renderers/shared/stack/reconciler/ReactRef.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,16 @@ var ReactRef = {};
function attachRef(ref, component, owner) {
if (typeof ref === 'function') {
var instance = component.getPublicInstance();
warning(
instance !== null,
'Stateless function components cannot have refs.'
);
if (__DEV__) {
var componentName = component && component.getName ?
component.getName() : 'a component';
warning(instance != null,
'Stateless function components cannot be given refs ' +
'(See %s%s).',
componentName,
owner ? ' created by ' + owner.getName() : ''
);
}
ref(instance);
} else {
// Legacy ref
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,19 @@ describe('ReactStatelessComponent', () => {

it('should warn for functional refs in pure functions', function() {
spyOn(console, 'error');
function Child() {
return <div/>;
}
ReactTestUtils.renderIntoDocument(<Child ref={node => {}} />);

var Parent = React.createClass({
displayName: 'Parent',
render: function() {
return <StatelessComponent name="A" ref={() => {}}/>;
},
});

ReactTestUtils.renderIntoDocument(<Parent />);
expect(console.error.calls.count()).toBe(1);
expect(console.error.calls.argsFor(0)[0]).toContain(
'Stateless function components cannot have refs.'
'Stateless function components cannot be given refs ' +
'(See StatelessComponent created by Parent).',
);
});

Expand Down

0 comments on commit 75f9efe

Please sign in to comment.