Skip to content

Instantly share code, notes, and snippets.

@imshines
Last active October 27, 2021 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imshines/4d28a0681939eaebbaf97378d0d9140c to your computer and use it in GitHub Desktop.
Save imshines/4d28a0681939eaebbaf97378d0d9140c to your computer and use it in GitHub Desktop.
React useRef Example
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