Skip to content

Instantly share code, notes, and snippets.

@JaysonChiang
Last active February 27, 2021 10:18
Show Gist options
  • Save JaysonChiang/5e8555f39d78c7a8b0482401f511d689 to your computer and use it in GitHub Desktop.
Save JaysonChiang/5e8555f39d78c7a8b0482401f511d689 to your computer and use it in GitHub Desktop.
Simple React Refs Sample
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