Skip to content

Refactor infinite scroll logic: update fetchMore to return hasMore and add setHasMore for better state management#19

Merged
pedrokarim merged 1 commit intomainfrom
dev
Mar 15, 2026
Merged

Refactor infinite scroll logic: update fetchMore to return hasMore and add setHasMore for better state management#19
pedrokarim merged 1 commit intomainfrom
dev

Conversation

@pedrokarim
Copy link
Owner

This pull request refactors the infinite scroll and data refresh logic in the gallery page to improve reliability and avoid unnecessary re-renders. The main changes involve switching from direct state dependencies to using refs for asynchronous functions and state, and updating the useInfiniteScroll hook API for better control and consistency.

Infinite scroll and data fetching improvements:

  • The useInfiniteScroll hook now expects fetchMore to return an object with { data, hasMore } instead of just an array, and takes an initialHasMore prop instead of hasMore. It exposes a new setHasMore method to update the "has more" state from the parent when needed. [1] [2]
  • The hook now manages its internal state with refs, avoiding unnecessary effect triggers and ensuring the "load more" logic is robust even when the sentinel remains visible after data updates. [1] [2]

Gallery page (GalleryClient) logic updates:

  • All async calls to fetchFiles and reset are now routed through refs (fetchFilesRef, resetRef) to prevent stale closures and unnecessary effect dependencies, leading to more predictable and performant updates. [1] [2] [3] [4] [5]
  • The gallery now uses the new setHasMore method from useInfiniteScroll to control pagination state after filters, uploads, or refreshes, ensuring correct behavior when the data set changes. [1] [2] [3] [4]
  • Effect dependencies and logic for filter/sort/search changes have been updated to use refs, reducing unnecessary reloads and making the code easier to maintain. [1] [2]

These changes collectively make the infinite scroll and data refresh logic more robust, maintainable, and less prone to subtle bugs caused by stale closures or unnecessary re-renders.

…d add setHasMore for better state management
@pedrokarim pedrokarim merged commit d4999a9 into main Mar 15, 2026
1 check 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.

1 participant