Created
April 21, 2021 23:21
-
-
Save nicksheffield/d3dae9aa7bb576b2c461b820f06bcf3b to your computer and use it in GitHub Desktop.
do auto focus on an html input more reliably in react
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 { 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