Skip to content

Instantly share code, notes, and snippets.

@tcodes0
Created July 26, 2019 22:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcodes0/c8215cbc79491be541e6d9c5bba8e3de to your computer and use it in GitHub Desktop.
Save tcodes0/c8215cbc79491be541e6d9c5bba8e3de to your computer and use it in GitHub Desktop.
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), []);
}
@tcodes0
Copy link
Author

tcodes0 commented Jul 26, 2019

@arthurdandrea good job

@tcodes0
Copy link
Author

tcodes0 commented Jul 26, 2019

related, more reliable streamich/react-use#154 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment