@barguide/react-hooks
Version:
TypeScript | React Hooks
29 lines (23 loc) • 591 B
text/typescript
import { useEffect } from 'react';
import throttle from 'lodash/throttle';
/**
* @name useScroll
* @description Pass this hook a callback to be fired in a
* throttled callback/handler
*/
const useScroll = (callback: () => void): void => {
// Setup
const throttledCallback = throttle(callback, 100, {
leading: true,
trailing: true
});
// Life Cycle
useEffect(() => {
window.addEventListener('scroll', throttledCallback);
callback();
return () => {
window.removeEventListener('scroll', throttledCallback);
};
}, []);
};
export { useScroll };