question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Receiving a warning when not passing a ref with controlled component

See original GitHub issue

Describe 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:closed
  • Created 2 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
coolRogercommented, Aug 12, 2021

Thank you for the reply @bluebill1049, and I checked out the source code of mantine:

The prop elementRef defined as elementRef?: React.ForwardedRef<HTMLInputElement>;, so I think it is doing the same thing like ref that keeps focus management to work.

1reaction
ahmedosama7450commented, May 7, 2021

The issue template is not very applicable here!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found