React Hook Form autofocus and error issue when used in HOC #8693
-
|
I have MUI TextField component(TextField2) inside of HOC, which is not getting auto-focused on form submission. I don't even get any errors on clicking of submit button for the first time. Any idea what I'm doing wrong? Stackblitz: https://stackblitz.com/edit/react-ts-1yn56f?file=App.tsx TestHoc.tsx : App.tsx : |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
|
Haven't looked at your code closely yet, but from what I see, the way you're using HOC is bad, meaning you re-init your component inside HOC every render, hence RHF looses the input's ref, hence you don't get the focus on your input. You should avoid initing your components / HOCs inside the func component's body during the render phase. It's not specific to how RHF works, but the general React behavior |
Beta Was this translation helpful? Give feedback.

Haven't looked at your code closely yet, but from what I see, the way you're using HOC is bad, meaning you re-init your component inside HOC every render, hence RHF looses the input's ref, hence you don't get the focus on your input.
You should avoid initing your components / HOCs inside the func component's body during the render phase. It's not specific to how RHF works, but the general React behavior