Last active
June 9, 2020 08:53
-
-
Save nirsky/1eeccc4f1c2268b5c18ae01831cf233f to your computer and use it in GitHub Desktop.
This file contains 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
//Class | |
class InputWithFocus extends React.Component { | |
constructor() { | |
super(); | |
this.inputRef = null; | |
} | |
render() { | |
return <div> | |
<input ref={inputRef => { this.inputRef = inputRef }} /> | |
<button onClick={() => this.inputRef.focus()}> | |
Focus the input | |
</button> | |
</div> | |
} | |
} | |
//Hooks | |
const InputWithFocus = (props) => { | |
const inputRef = useRef(); | |
return <div> | |
<input ref={inputRef} /> | |
<button onClick={() => inputRef.current.focus()}> | |
Focus the input | |
</button> | |
</div> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment