Created
July 26, 2019 22:41
-
-
Save tcodes0/c8215cbc79491be541e6d9c5bba8e3de to your computer and use it in GitHub Desktop.
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 throttle from 'lodash/throttle'; | |
import { useRef, useEffect, useCallback } from 'react'; | |
export default function useThrottle(func, time = 1000) { | |
const throttleRef = useRef(); | |
const callbackRef = useRef(); | |
useEffect(() => { | |
callbackRef.current = func; | |
}, [func]); | |
useEffect(() => { | |
throttleRef.current = throttle((...args) => callbackRef.current(...args), time); | |
}, [time]); | |
return useCallback((...args) => throttleRef.current(...args), []); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@arthurdandrea good job