Skip to content

Instantly share code, notes, and snippets.

@treyhuffine
Created March 5, 2019 01:14
Show Gist options
  • Save treyhuffine/4117ba06a03819e10f826a1746acb42a to your computer and use it in GitHub Desktop.
Save treyhuffine/4117ba06a03819e10f826a1746acb42a to your computer and use it in GitHub Desktop.
function TextInputWithFocusButton() {
// The type of our ref is an input element
const inputEl = useRef<HTMLInputElement>(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>
</>
);
}
@ayushbpl10
Copy link

With type script 3.5.3 :
Had to change
inputEl.current!.focus();

non-null assertion operator !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment