react-hooks-bank
Version:
A collection of **powerful, reusable custom React hooks** for complex, non-trivial interactions that go beyond React’s native features.
28 lines (27 loc) • 1.07 kB
JavaScript
import { useEffect, useState } from "react";
export function useInfiniteScroll(callback, threshold) {
if (threshold === void 0) { threshold = 100; }
var _a = useState(false), isLoading = _a[0], setIsLoading = _a[1];
useEffect(function () {
var handleScroll = function () {
if (window.innerHeight + document.documentElement.scrollTop >=
document.documentElement.offsetHeight - threshold) {
if (!isLoading) {
setIsLoading(true);
try {
callback();
}
catch (error) {
console.error("Error in callback:", error);
}
finally {
setIsLoading(false);
}
}
}
};
window.addEventListener("scroll", handleScroll);
return function () { return window.removeEventListener("scroll", handleScroll); };
}, [isLoading, callback, threshold]);
return isLoading;
}