I have a page where a list of profiles are shown and when the user scrolls to the bottom of the page, the query should load more data. The loadMore function works as expected but my error is in the listener as when the user scrolls to the bottom quickly, it gets called more than 5 times.
const listener = useCallback(() => {
if (window.innerHeight + window.scrollY >= document.body.scrollHeight) {
loadMore();
}
}, [loadMore]);
useEffect(() => {
window.addEventListener('scroll', listener);
return () => window.removeEventListener('scroll', listener);
}, [listener]);
This causes the same items in the new query to be shown as it sends the loadMore the same data.
When i slowly scroll it works. When I scroll to the bottom fast loads the same profiles 5 different times. Its in the if statement of the listener.