-
Notifications
You must be signed in to change notification settings - Fork 720
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf(demo): lazy render example tiles #1896
base: master
Are you sure you want to change the base?
Conversation
observer.unobserve(curr); | ||
} | ||
}; | ||
}, [ref]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, might not want to use a ref as an effect dep. It'll be stable across re-renders and not trigger the effect. Can use useCallback()
. https://legacy.reactjs.org/docs/hooks-faq.html#how-can-i-measure-a-dom-node
}, []); | ||
|
||
return { everVisible, ref }; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about this hook instead?
function useIntersectionObserver() {
const [entry, setEntry] = useState<IntersectionObserverEntry | null>(null);
const prevObserver = useRef<IntersectionObserver | null>(null);
const ref = useCallback((node: Element) => {
if (prevObserver.current) {
prevObserver.current.disconnect();
prevObserver.current = null;
}
const observer = new IntersectionObserver(
([entry]) => {
setEntry(entry);
},
{ threshold: 0.01, root: null },
);
observer.observe(node);
prevObserver.current = observer;
}, []);
return [ref, entry];
}
This could be a better approach for a few reasons:
- I think
useIntersectionObserver
as a name feels like it more directly reflects its functionality, whileuseEverVisible
is more abstract. - I think using
disconnect
is better because it completely removes the observer and stops watching all elements, ensuring proper cleanup, whereasunobserve
only stops watching a specific element without cleaning up the observer itself, which can lead to memory leaks if not handled properly. - We can return the full
IntersectionObserverEntry
, giving access to more useful visibility data (maybe you don't want or care about this though since we're only using it to check if an element is visible or not). - We are only observing one element at a time in the hook, so even though the callback of the
IntersectionObserver
always returns an array of entries, the array will always have just one entry, so we don't need to loop over every entry. - The hook has fewer concerns about state tracking and effects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dig it! this is a great suggestion for all the reasons you listed, I was just being lazy (hah!) :)
🚀 Enhancements
Improve the performance of the initial gallery page load by lazy rendering visible tiles