Function components cannot be given refs.
See original GitHub issueHello! I’d like to move from styled-components to goober, but can’t fix one issue:
Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
Here is example: https://codesandbox.io/s/react-goober-forked-w3dh2?file=/src/index.js
i’ve tried many things, but console.log(ref)
is always undefined, instead of el
What i’m doing wrong?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:8 (5 by maintainers)
Top Results From Across the Web
How do I avoid 'Function components cannot be given refs ...
Just give it as innerRef , // Client.js <Input innerRef={inputRef} />. Use it as ref . // Input.js const Input = ({ innerRef...
Read more >Solved: Function components cannot be given refs. ...
It will not generate an error of Function components cannot be given refs. /* Child.jsx */ import React from 'react' class Child extends...
Read more >ref prop should not be used on React function components
This rule applies when a ref prop is specified on a React function component. The ref prop is used to provide a reference...
Read more >Forwarding Refs
Regular function or class components don't receive the ref argument, and ref is not available in props either. Ref forwarding is not limited...
Read more >warning: function components cannot be given refs. ...
Iget error 'Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?' . How...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi @Crisis2010, thanks for opening this issue.
Since
goober
is agnostic of your virtual dom, jsx, library of choice,styled
supports a second argument that is the reference for your libraryforwardRef
API. In your case you need to passReact.forwardRef
as the second argument to the components that you need your ref to:Let me know if this helps. Cheers!
Awesome work @mpkelly! Thanks for opening the PR.
As for the
.attrs
api, there’s no equivalent in goober. I believe there should be a hard line between Styled components and regular ones and I feel strange about.attrs
😄 it’s like a pre-processor for your props but shouldn’t that functionality exist inside the functional components instead? I don’t know, I really think we should keep these things separated.Anyway in goober the equivalent would be to have a
InputStyled
that handles the styling and the mainInput
component handling the type and size. Like so:Running the above in here https://codesandbox.io/s/loving-gareth-n3vtx?file=/src/App.js:23-403