Próbuję napisać infinite scroll w React i typeScript. Komponent jest ogólnie długi więc wkleje tylko najważniejsze rzeczy.
Póki co próbuję osiągnąć stan, w którym konsola wypisywałaby mi cokolwiek, jeśli tylko <ClipLoader /> pojawi się na ekranie użytkownika. Oczywiście ponad nim są rzeczy tylko tutaj je wyciąłem, aby wyrzucić zbędny kod.
Na chwilę obecną typeScript wyrzuca mi taki błąd :
Argument of type 'null' is not assignable to parameter of type 'Element'.ts(2345)
w liniach :
if (loader && loader.current) observer.observe(loader.current);
[...]
if (loader.current) observer.unobserve(loader.current);
Podkreślone są load.current jako argumenty do observer.observe/unobserve...
const loader = useRef(null);
const loadMore = useCallback((entries) => {
console.log(entries);
if (entries[0].isIntersecting) console.log(entries[0].isIntersecting);
}, []);
useEffect(() => {
const options = {
root: null,
rootMargin: "0px",
threshold: 0.1,
};
const observer = new IntersectionObserver(loadMore, options);
if (loader && loader.current) observer.observe(loader.current);
return () => {
if (loader.current) observer.unobserve(loader.current);
};
});
[...]
return (
[...]
{!loading && <ClipLoader ref={loader} color={"#276a39"} />}
)