Created
October 12, 2023 15:23
-
-
Save itsmacr8/c9c8f9c4ec868e1ef76bb6ac4d1e7725 to your computer and use it in GitHub Desktop.
How to use useRef hook basic example.
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
import { useRef } from 'react'; | |
function App() { | |
const formInputRef = useRef(null) | |
const focusInput = () => { | |
formInputRef.current.focus(); | |
} | |
return ( | |
<> | |
<h1>Using useRef hook to access underlying DOM</h1> | |
<input ref={formInputRef} type="text" /> | |
<button onClick={focusInput}>Focus input</button> | |
</> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment