Skip to content

Instantly share code, notes, and snippets.

@nicksheffield
Created April 21, 2021 23:21
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 nicksheffield/d3dae9aa7bb576b2c461b820f06bcf3b to your computer and use it in GitHub Desktop.
Save nicksheffield/d3dae9aa7bb576b2c461b820f06bcf3b to your computer and use it in GitHub Desktop.
do auto focus on an html input more reliably in react
import { useEffect, useRef } from "react"
const useAutoFocus = (enabled: boolean) => {
const ref = useRef<HTMLElement>()
useEffect(() => {
if (ref.current && enabled) {
setTimeout(() => ref.current?.focus(), 100)
}
}, [enabled])
return ref
}
export default useAutoFocus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment