I'm trying to throttle an event "wheel" on scroll using a lodash library for my React app with no success.
I need to listen to e.deltaY from scroll input in order to detect its scroll direction. To add a listener I wrote a React hook that accepts an event name and a handler function.
Base implementation
const [count, setCount] = useState(0);
const handleSections = () => {
setCount(count + 1);
};
const handleWheel = _.throttle(e => {
handleSections();
}, 10000);
useEventListener("wheel", handleWheel);
My useEventListener hook
function useEventListener(e, handler, passive = false) {
useEffect(() => {
window.addEventListener(e, handler, passive);
return function remove() {
window.removeEventListener(e, handler);
};
});
}
Working demo: https://codesandbox.io/s/throttledemo-hkf7n
My goal is to throttle this scroll event in order to have less events firing and to have a few seconds to scroll my page programmatically (scrollBy(), par example).
At this moment throttling seem to not work so I'm getting lots of scroll events all at once