I am looking at the documentation from react regarding omitting functions from the list of dependencies because I got this error in my js linter. I have tried to amend my code like the docs show, but I feel like I am missing something, because I still get the same error. I will post my code before and after my attempt. Thanks.
warning React Hook useEffect has a missing dependency: 'handleDocumentScrollThrottled'. Either include it or remove the dependency array react-hooks/exhaustive-deps
Code Before attempted fix
/* Completed useDocumentScrollThrottled utility function */
import { useEffect, useState } from 'react';
import { throttle } from 'lodash';
function useDocumentScrollThrottled(callback) {
const [, setScrollPosition] = useState(0);
let previousScrollTop = 200;
function handleDocumentScroll() {
const { scrollTop: currentScrollTop } = document.documentElement || document.body;
setScrollPosition(previousPosition => {
previousScrollTop = previousPosition;
return currentScrollTop;
});
callback({ previousScrollTop, currentScrollTop });
}
const handleDocumentScrollThrottled = throttle(handleDocumentScroll, 250);
useEffect(() => {
window.addEventListener('scroll', handleDocumentScrollThrottled);
return () =>
window.removeEventListener('scroll', handleDocumentScrollThrottled);
}, []);
}
export default useDocumentScrollThrottled;
Code after attempted fix
...
useEffect(() => {
function doSomething(){
window.addEventListener('scroll', handleDocumentScrollThrottled);
return () =>
window.removeEventListener('scroll', handleDocumentScrollThrottled);
}
doSomething();
}
...