Skip to content

fix(hooks): stop IntersectionObserver recreating on every loading tog… - #1246

Merged
RUKAYAT-CODER merged 1 commit into
rinafcode:mainfrom
abdulazizishaq212-prog:fix/infinite-scroll-observer-recreate-898
Aug 26, 2026
Merged

fix(hooks): stop IntersectionObserver recreating on every loading tog…#1246
RUKAYAT-CODER merged 1 commit into
rinafcode:mainfrom
abdulazizishaq212-prog:fix/infinite-scroll-observer-recreate-898

Conversation

@abdulazizishaq212-prog

Copy link
Copy Markdown
Contributor

Root cause: runLoadMore depended on loading via useCallback, and the observer effect depended on both. Every loading flip → new runLoadMore →
observer effect re-runs → observer torn down and recreated mid-scroll.

Fix — two refs break the chain:

  • loadingRef — synced to loading via a small useEffect. The observer callback reads it directly, no longer needing loading as a dep.
  • runLoadMoreRef — updated every render (no dep array). The observer callback always calls the latest version without listing it as an effect
    dep.

Observer effect deps are now only: [hasNextPage, rootMargin, threshold] — the only things that should legitimately recreate the observer.

Branch fix/infinite-scroll-observer-recreate-898 is pushed. Use the PR description above when opening the PR on GitHub at the URL the push
printed.

closes #898

…gle (rinafcode#898)

- Add loadingRef (synced to loading state via useEffect) so the observer
  callback can read the latest loading value without a dep on the state var.
- Add runLoadMoreRef (updated on every render, no deps) so the observer
  callback always invokes the latest runLoadMore without listing it as a dep.
- Remove loading and runLoadMore from the observer effect dep array; deps
  are now only [hasNextPage, rootMargin, threshold].
- Remove loading from runLoadMore's useCallback dep array (read via ref).

Result: the IntersectionObserver is only torn down and recreated when
hasNextPage, rootMargin, or threshold actually change — not on every
loading state flip, which was causing missed/duplicate callbacks during
rapid scrolling.
@drips-wave

drips-wave Bot commented Aug 26, 2026

Copy link
Copy Markdown

@abdulazizishaq212-prog Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@RUKAYAT-CODER

Copy link
Copy Markdown
Contributor

Thank you for contributing to the project.

@RUKAYAT-CODER
RUKAYAT-CODER merged commit 5c5f4de into rinafcode:main Aug 26, 2026
6 checks passed
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.

Stop useInfiniteScroll from recreating the IntersectionObserver on every load toggle

2 participants