-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuseInfiniteLoader.js
65 lines (61 loc) · 1.66 KB
/
useInfiniteLoader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React from "react";
export default function useInfiniteLoader({
startFromPage = 0,
loadMore,
canLoadMore = false,
initialise = true,
rootMargin = "100px 0px 0px 0px",
threshold = 0,
debug = false,
}) {
function log(...args) {
if (debug) {
// eslint-disable-next-line no-console
console.log(...args);
}
}
if (typeof loadMore !== "function") {
throw new TypeError(
"useInfiniteLoader: loadMore must be a function and is required"
);
}
const loaderRef = React.useRef(null);
const page = React.useRef(startFromPage);
const observer = React.useRef(null);
function resetPage() {
page.current = startFromPage;
}
React.useEffect(() => {
if (!observer.current && initialise === true) {
log("Initialised");
observer.current = new IntersectionObserver(
([target]) => {
log("Observer invoked");
if (target.intersectionRatio <= 0) {
log("Intersection ratio not met, bailing");
return;
}
if (canLoadMore === false) {
log("Can load more is false, bailing");
return;
}
log("Loading more...");
loadMore(page.current);
page.current += 1;
},
{ rootMargin, threshold }
);
if (loaderRef.current) {
log("Observing loader ref");
observer.current.observe(loaderRef.current);
}
}
return () => {
if (observer && observer.current) {
observer.current.disconnect();
observer.current = undefined;
}
};
}, [canLoadMore, loadMore, page, initialise]);
return { loaderRef, page: page.current, resetPage };
}