import { useCallback , useEffect , useRef } from 'react' ; type Callback < T > = ( ... args : T [ ] ) => void ; export function useEventCallback < T > ( fn ) { const ref = useRef < Callback < T > > ( fn ) ; useEffect ( ( ) => { ref . current = fn ; } ) ; return useCallback ( ( ... args ) => { const fun = ref . current ; return fun ( ... args ) ; } , [ ] ) ; }