Receiving a warning when not passing a ref with controlled component
See original GitHub issueDescribe the bug
I am trying to integrate react hook form with Headless UI Select Component
This component is built from scratch, It doesn’t have an underlying native html select tag, I guess. So, I don’t have access to a ref
. It only exposes onChange
and value
(API Reference).
I am using the hook form Controller
to wrap a custom select like this
<Controller
name='year'
control={control}
render={({ field }) => {
return (
<CustomSelect
value={field.value}
onChange={(value)=> field.onChange(value)}
label='Year'
className='mt-5 sm:flex-1'
options={[
{
value: 1,
text: 'First Year',
},
{
value: 2,
text: 'Second Year',
},
]}
/>
);
}}
/>
I think everything is working just fine except that I receive this warning in chrome devtools
Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
Check the render method of `Controller`.
at CustomSelect (http://localhost:3000/_next/static/chunks/pages/create-profile.js?ts=1620420857237:473:22)
at Controller (http://localhost:3000/_next/static/chunks/pages/create-profile.js?ts=1620420857237:40059:37)
at form
at div
at div
at main
at div
at CreateProfile (http://localhost:3000/_next/static/chunks/pages/create-profile.js?ts=1620420857237:46240:81)
....
I think that you require passing ref
in controlled components for things like focusing the component. I don’t really need this and also this component doesn’t support that, I guess.
To Reproduce Use headless UI select component with hook form
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
How do I avoid 'Function components cannot be given refs ...
Warning : Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?. Check the ......
Read more >Uncontrolled Components - React
In a controlled component, form data is handled by a React component. The alternative is uncontrolled components, where form data is handled by...
Read more >ForwardRef issue when using <Controller> #3411 - GitHub
Getting console.errors from React i.e. Warning: Function components cannot be given refs. Attempts to access this ref will fail.
Read more >warning: function components cannot be given refs. attempts ...
Solution 1: As the warning state you cannot assign refs to functional component without the usage the forwardRef In order to have access...
Read more >A component is changing an uncontrolled input to be controlled
To fix the warning, initialize the input value to an empty string, ... Passing a prop like value={undefined} is the same as not...
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 FreeTop 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
Top GitHub Comments
Thank you for the reply @bluebill1049, and I checked out the source code of mantine:
The prop
elementRef
defined aselementRef?: React.ForwardedRef<HTMLInputElement>;
, so I think it is doing the same thing likeref
that keeps focus management to work.The issue template is not very applicable here!