Created
March 5, 2019 01:14
-
-
Save treyhuffine/4117ba06a03819e10f826a1746acb42a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |
</> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With type script 3.5.3 :
Had to change
inputEl.current!.focus();
non-null assertion operator !