Focus An Input With useRef Hook
Last updated
Was this helpful?
Last updated
Was this helpful?
You can send focus to an input by calling on it. To do this in a React context, you need to have a reference to the underlying DOM node. You can get this reference with the .
When this component mounts, the underlying <input>
element will get tied to inputRef
. The focusInput
handler that I've created can be used to send focus to the corresponding inputRef
. To demonstrate, I've passed it to the onClick
handler of the button. Clicking the button will focus the input.