Skip to content

Instantly share code, notes, and snippets.

@programarivm
Created May 1, 2023 22:08
Show Gist options
  • Save programarivm/75e42876e4fa20b09c86559eae3c2fb6 to your computer and use it in GitHub Desktop.
Save programarivm/75e42876e4fa20b09c86559eae3c2fb6 to your computer and use it in GitHub Desktop.
Select an element
function TextInputWithFocusButton() {
const inputEl = useRef(null);
const onButtonClick = () => {
// `current` points to the mounted text input element
inputEl.current.focus();
};
return (
<>
<input ref={inputEl} type="text" />
<button onClick={onButtonClick}>Focus the input</button>
</>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment