-
Notifications
You must be signed in to change notification settings - Fork 47.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Warn for functional refs on stateless functional components #7272
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
'use strict'; | ||
|
||
var ReactOwner = require('ReactOwner'); | ||
var warning = require('warning'); | ||
|
||
import type { ReactInstance } from 'ReactInstanceType'; | ||
import type { ReactElement } from 'ReactElementType'; | ||
|
@@ -21,7 +22,18 @@ var ReactRef = {}; | |
|
||
function attachRef(ref, component, owner) { | ||
if (typeof ref === 'function') { | ||
ref(component.getPublicInstance()); | ||
var instance = component.getPublicInstance(); | ||
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() : '' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO this should follow the normal pattern |
||
); | ||
} | ||
ref(instance); | ||
} else { | ||
// Legacy ref | ||
ReactOwner.addComponentAsRefTo( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,7 +146,25 @@ describe('ReactStatelessComponent', () => { | |
); | ||
}); | ||
|
||
it('should warn when given a ref', () => { | ||
it('should warn for functional refs in pure functions', function() { | ||
spyOn(console, 'error'); | ||
|
||
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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably better to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I use |
||
'Stateless function components cannot be given refs ' + | ||
'(See StatelessComponent created by Parent).', | ||
); | ||
}); | ||
|
||
it('should warn when given a ref', function() { | ||
spyOn(console, 'error'); | ||
|
||
class Parent extends React.Component { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
component.getName
is not always going to be defined here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah okay, I guess you can remove
component && ...
though