Warn for functional refs on stateless functional components#7272
Warn for functional refs on stateless functional components#7272aweary wants to merge 3 commits intofacebook:masterfrom
Conversation
830bd13 to
904945a
Compare
| var instance = component.getPublicInstance(); | ||
| warning( | ||
| instance !== null, | ||
| 'Stateless function components cannot have refs.' |
There was a problem hiding this comment.
Maybe keep it consistent with the docs and name it "Stateless functional components"?
There was a problem hiding this comment.
This warning is consistent with the current invariant in ReactCompositeComponent: https://github.com/facebook/react/blob/master/src/renderers/shared/stack/reconciler/ReactCompositeComponent.js#L1182
There was a problem hiding this comment.
But I do think I should probably include the component/ref name just like https://github.com/facebook/react/blob/master/src/renderers/shared/stack/reconciler/ReactCompositeComponent.js#L1187-L1190
There was a problem hiding this comment.
Yes, knowing which component has the invalid ref would help a lot.
|
What if I pass a function from parent and use it for the ref? String refs won't work. But I think function refs do. |
|
@satya164 do you mean like passing a function down to a child via the If it's a stateless component it will always pass |
|
@aweary Ah. |
| 'Stateless function components cannot be given refs ' + | ||
| '(See %s%s).', | ||
| componentName, | ||
| owner ? ' created by ' + owner.getName() : '' |
There was a problem hiding this comment.
IMO this should follow the normal pattern check the render method of ...
Something like Stateless functional component %s cannot have refs attached. Check the render method of %s. I also think that the other message (when using a string ref) should follow this pattern too.
|
Someone from FB should probably review this too. function A() {
return <B ref="B-ref" /> // invariant called
return <B ref={() => {}} /> // invariant is not called
} |
@edvinerikson React should already warn for return <B ref="B-ref" />This PR actually implements what I think you're describing, if I'm understanding you correctly. If you look at the test, you can see what this PR is addressing: return <StatelessComponent name="A" ref={() => {}}/>;Currently, React will call the |
|
What I tried to explain is that if you use class AClass extends Component {}
function FunctaionalComponent() {
return <AClass ref={() => {}} />
// this won't warn right now but it should because using refs inside a stateless
// component shouldn't be possible, right? (Not possible with string refs at least)
}Edit: |
|
@edvinerikson thanks for clarifying, that should be doable by checking the instance for |
|
It's currently failing on a couple tests using It seems like |
I guess that maybe you can check if |
Yeah, I'd hate to do that just because of |
|
Someone from FB will probably need to decide what to do. |
It is possible for callback refs. class AClass extends Component {}
function FunctaionalComponent() {
var inst;
return <AClass ref={a => inst = a} />
}This is why callback refs are better than string refs. |
|
@aweary Would you like to revive this? |
0ade166 to
75f9efe
Compare
|
@gaearon rebased. There are a few failing tests for |
|
Ping :-) |
|
Actually I think this is superseded by #8635 (which also adds them for Fiber). |
Resolves #7267
Currently React warns if you do:
But it doesn't warn if you do
It seems like a generally good idea to have a consistent warning when using
refswith any SFC.