I am trying to make application similar to facebook. When I reach bottom of the page I am fetching more data from API. I am using React and Redux. Simplified code:
const detectBottomOfPage= () => {
if((window.innerHeight + window.scrollY) > document.body.scrollHeight) {
dispatch(bottomOfPage()); //increases scrollCount
}
};
useEffect(() => {
window.addEventListener('scroll', detectBottomOfPage);
}, []);
useEffect(() => {
dispatch(fetchMoreData(scrollCount));
},[scrollCount]);
When i scroll down it works fine, and fetches data. (page increases in height). But when i refresh the page, it detects bottom of page multiple times. I tried to remove listener before refresh (beforeunload event) but it doesn't work properly after refresh. Any solution ?