12 lines
372 B
TypeScript
12 lines
372 B
TypeScript
import { useEffect, useRef } from 'react'
|
|
|
|
export function useFocusCallback(callback: () => void) {
|
|
const callbackRef = useRef(callback)
|
|
callbackRef.current = callback
|
|
|
|
useEffect(() => {
|
|
const handleFocus = () => callbackRef.current()
|
|
window.addEventListener('focus', handleFocus)
|
|
return () => window.removeEventListener('focus', handleFocus)
|
|
}, [])
|
|
}
|