1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 15:18:01 +00:00
Files
til/react/focus-an-input-with-useref-hook.md
2020-05-19 16:36:05 -05:00

37 lines
1.0 KiB
Markdown

# Focus An Input With useRef Hook
You can send focus to an input by calling
[`focus()`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLOrForeignElement/focus)
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 [`useRef`
hook](https://reactjs.org/docs/hooks-reference.html#useref).
```jsx
import React, { useRef } from 'react';
const MyComponent = () => {
const inputRef = useRef(null);
const focusInput = () => {
inputRef.current.focus();
}
return (
<div>
<input
ref={inputRef}
type="text"
value=""
{...otherProps}
/>
<button onClick={focusInput}>Edit Input</button>
</div>
);
}
```
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.