Intersection observer on Safari #548
-
Hi guys, nice lib!! export function InfiniteScroll({loadMore, hasMore, isLoading, children}) {
const {ref, inView} = useInView({
threshold: 0.9,
});
React.useEffect(() => {
if (inView && !isLoading && hasMore) {
loadMore();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [inView]);
return (
<Box>
{children}
{!isLoading && (
// Trick to fix IntersectionObserver not working in Safari when component is empty
<span ref={ref} style={{color: "white"}}>
a
</span>
)}
</Box>
);
} I've to use the following "Hack" because on Safari when the component is empty the IntersectionObserver API doesn't work properly. ......
{!isLoading && (
// Trick to fix IntersectionObserver not working in Safari when component is empty
<span ref={ref} style={{color: "white"}}>
a
</span>
)}
........ Any suggestions? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Can you define "not working in Safari"? How are you intending to use the |
Beta Was this translation helpful? Give feedback.
-
My guess would be it never triggers because the
It might also make sense for you to use the new |
Beta Was this translation helpful? Give feedback.
-
Hey @thebuilder, Thx for the advices, I was left with a solution like this: export function InfiniteScroll({loadMore, hasMore, isLoading, children}) {
const {ref, inView} = useInView({
threshold: 0.9,
onChange: (inView) => {
if (inView && !isLoading && hasMore) {
loadMore();
}
},
});
return (
<Box>
{children}
{!isLoading && (
<span ref={ref}>
</span>
)}
</Box>
);
} All work well on Safari!! |
Beta Was this translation helpful? Give feedback.
My guess would be it never triggers because the
<span>
is empty.threshold: 0
?<div>
?It might also make sense for you to use the new
onChange
option - It would allow you to remove theuseEffect
:https://github.com/thebuilder/react-intersection-observer#options