Search code examples
reactjsreact-hook-formyup

How to solve the issue of unfocused Input created dynamically using react-hook-form with yup


I created a form and I want that when the user create a field the focus will be on the last one. In practice what is happening it skips to the input that not created dynamicly and this input get the focus .

Click on the + button and you will see that the textarea got focus :

code

I want the focus to be on the last dynamically created element, not on the textarea.

thanks for your help !


Solution

  • By using document.getElementById(lastFields.id).focus() in useEffect

    add this:
    enter image description here

    update this:
    enter image description here