auto-focus.js 374 B

123456789101112131415161718
  1. import { createRef, useEffect } from 'react'
  2. export function useRefWithAutoFocus() {
  3. const autoFocusedRef = createRef()
  4. useEffect(
  5. () => {
  6. if (autoFocusedRef.current) {
  7. requestAnimationFrame(() => {
  8. if (autoFocusedRef.current) autoFocusedRef.current.focus()
  9. })
  10. }
  11. },
  12. [autoFocusedRef]
  13. )
  14. return { autoFocusedRef }
  15. }