Last active
October 27, 2021 04:24
-
-
Save imshines/4d28a0681939eaebbaf97378d0d9140c to your computer and use it in GitHub Desktop.
React useRef 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 React from 'react'; | |
import './style.css'; | |
export default function App() { | |
const inputRef = React.useRef(); // Creating a ref | |
React.useEffect(() => { | |
inputRef.current.focus(); // focus on input on compoenent mount | |
}, []); | |
return ( | |
<div> | |
<input type="text" ref={inputRef} /> {/* add ref to the input element */} | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment