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.

ForwardRef issue when using <Controller>

See original GitHub issue

Describe the bug After updating to 6.11.0 I am facing an issue when I am using <Controller>

To Reproduce Steps to reproduce the behavior:

  1. Go to ‘https://codesandbox.io/s/react-hook-form-controller-forked-drdv2?file=/package.json
  2. Click on ‘open console’
  3. Scroll down to ‘…’
  4. Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

Codesandbox link (Required) https://codesandbox.io/s/react-hook-form-controller-forked-drdv2?file=/package.json

Screenshots If applicable, add screenshots to help explain your problem. Screen Shot 2020-11-10 at 18 58 40 Screen Shot 2020-11-10 at 18 58 48

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:15
  • Comments:22 (8 by maintainers)

github_iconTop GitHub Comments

7reactions
tafelitocommented, Nov 11, 2020

@bluebill1049 this does not work when using the as prop of the Controller

I updated your example, you can see the error there

https://codesandbox.io/s/wandering-wood-bcmm7?file=/src/App.tsx

5reactions
bluebill1049commented, Nov 10, 2020

Controller now has ref prop for better focus management and accessibility. eg now React-Select works out the box with auto focus.

https://codesandbox.io/s/focused-sammet-tr193

<Controller
  control={control}
  name="test"
  render={({ onChange, onBlur, value, name, ref }) => (
    <TextField
      onBlur={onBlur}
      onChange={onChange}
      checked={value}
      inputRef={ref}
    />
  )}
/>

it’s best to wire up the ref yourself, eg: TextField could be inputRef so we can focus that input when an error occurred, or you can omit that ref prop.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React hook form v7 Function components cannot be given refs ...
So the issue is that I think that the {...register("name"}} line actually ... forwardRef() solves the problem when using Custom Components.
Read more >
warning: function components cannot be given refs. attempts ...
8. As the warning states you cannot assign refs to functional components without the usage of forwardRef. In order to have access to...
Read more >
ref prop should not be used on React function components
If the intention is to refer an inner element of the function component, you can use React.forwardRef() instead.
Read more >
Circular Dependency - A progressive Node.js framework
An alternative to using forwardRef() is to refactor your code and use the ModuleRef class to retrieve a provider on one side of...
Read more >
How to avoid circular dependencies in NestJS - LogRocket Blog
Learn about the issues caused by circular dependencies in Nestjs, ... Now, say we use the UserService as follows in the UserController class ......
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