import { useRef , useEffect , useCallback } from "react" ; type Callback = ( ... args : any ) => void ; export default ( callback , delay , dependencies = [ ] ) => { const throttleRef = useRef < Boolean > ( false ) ; const callbackRef = useRef < Callback > ( callback ) ; useEffect ( ( ) => { callbackRef . current = callback ; } , [ callback ] ) ; return useCallback < Callback > ( ( ... args ) => { if ( throttleRef . current ) return ; throttleRef . current = true ; callbackRef . current ( ... args ) ; setTimeout ( ( ) => { throttleRef . current = false ; } , delay ) ; } , [ delay , ... dependencies ] ) ; } ;