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.

Controlled components do not get auto-focused on submit

See original GitHub issue

Describe the bug When using uncontrolled components, handleSubmit focuses the first invalid field in the form. Example screenshot:

uncontrolled

However, for controlled components using <Controller> the fields do not get focused. Example screenshot:

controlled

To Reproduce Steps to reproduce the behavior:

  1. Use a controlled component with <Controller>
  2. Trigger handleSubmit

Codesandbox link https://codesandbox.io/s/beautiful-worker-22hi3

Expected behavior Controlled components receive focus in the same way uncontrolled components do.

Desktop (please complete the following information):

  • OS: macOS 10.15.3
  • Browser Chrome
  • Version 80.0.3987.116 (Official Build) (64-bit)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
bluebill1049commented, Mar 30, 2020

Ah, so this just is not supported at all with the Controller or “controlled” components at the moment?

That’s not precise as well, because you can do it yourself at useEffect as long as you get to hold of the errors and ref (controlled input).

useEffect(() => {
  if (errors.text) {
    this.input.ref.focus();
  }
}, [errors])

anyway, let’s looking forward for the next update.

2reactions
xiaoyu-tamucommented, Feb 29, 2020

image

Read the doc

Read more comments on GitHub >

github_iconTop Results From Across the Web

3 ways to autofocus an input in React that ALMOST always work!
First off, what is focus? This input is focus ed, but no text is selected. If the user starts typing, the text they...
Read more >
Set focus on input field of the form in controlled component
Here is the on submit handler. I want to set focus on input field. Please tell me the more right way doing this...
Read more >
How to autofocus using React Hooks - LogRocket Blog
The first way we can get more control over autofocus is with the useCallback() Hook. Here is the same form with our new...
Read more >
4 Ways to Set Focus on an Input Field After Rendering in React
Autofocus using class component. First, we need to make our App.js file a class component. To do so, just paste the below code...
Read more >
autofocus - HTML: HyperText Markup Language | MDN
If applied to multiple elements the first one will receive focus. Note: The autofocus attribute applies to all elements, not just form controls....
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