Last active
February 27, 2021 10:18
-
-
Save JaysonChiang/5e8555f39d78c7a8b0482401f511d689 to your computer and use it in GitHub Desktop.
Simple React Refs Sample
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 { useState, useRef, useEffect } from 'react'; | |
const Search = () => { | |
const inputRef = useRef(null); | |
const [name, setName] = useState(''); | |
useEffect(() => { | |
inputRef.current.focus(); | |
}, []); | |
return ( | |
<div> | |
<input | |
ref={inputRef} | |
value={name} | |
onChange={(e) => setName(e.target.value)} | |
/> | |
<div>{name}</div> | |
</div> | |
); | |
}; | |
export default Search; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment