Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 ofIntersectionObserver
are never disconnected/unobserved.A way to address this is to just use one instance of
IntersectionObserver
and tounobserve
the elements either once they are intersected or when they are unmounted (using the cleanup function ofuseEffect
). 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.