Skip to content
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

Improve useNearScreen implementation #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

josepot
Copy link

@josepot josepot commented Aug 17, 2019

There is a small problem with the current implementation of useNearScreen, which is that if a user navigates to a different page without having seen all the all the "observed elements", I believe that can create a small memory leak because those instances of IntersectionObserver are never disconnected/unobserved.

A way to address this is to just use one instance of IntersectionObserver and to unobserve the elements either once they are intersected or when they are unmounted (using the cleanup function of useEffect). I think that this change could improve the performance of this hook.

Also, notice that with these changes if the polyfill is needed, then its download would start significantly earlier. Therefore, decreasing the risk of an element being intersected before its observer is created. Unless I'm mistaken, with the current implementation the download of the polyfill would start after the root component is mounted. With these changes the download should start while the "App/Main" js asset is being parsed.

@vercel
Copy link

vercel bot commented Aug 17, 2019

This pull request is automatically deployed with Now.
To access deployments, click Details below or on the icon next to each push.

Latest deployment for this branch: https://react-rendering-git-fork-josepot-fix-memory-leak.midudev.now.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant