react-snp-infinite-scroller
Version:
Make window or an element call a function each time that it reaches to the end of the page
20 lines (18 loc) • 509 B
JavaScript
import { useEffect } from 'react';
export default ({ callback = () => {}, hasMore = true, threshold = 100 }) => {
const handleScroll = () => {
if (
document.body.scrollHeight <=
window.innerHeight + window.pageYOffset + threshold &&
hasMore
) {
callback();
}
};
useEffect(() => {
window.addEventListener('scroll', handleScroll);
return () => {
window.removeEventListener('scroll', handleScroll);
};
}, [hasMore,threshold]);
};