fix: update fetchNextPage condition in Example component#9626
fix: update fetchNextPage condition in Example component#9626TkDodo merged 1 commit intoTanStack:mainfrom
Conversation
WalkthroughUpdated infinite scroll logic in the React example to conditionally trigger fetchNextPage only when the sentinel is in view, a next page is available, and no fetch is currently in progress. The effect’s dependency array now includes inView, hasNextPage, isFetchingNextPage, and fetchNextPage. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant U as User
participant V as Viewport/Observer
participant P as Page Component
participant RQ as React Query
U->>V: Scrolls
V-->>P: inView = true
alt inView && hasNextPage && !isFetchingNextPage
P->>RQ: fetchNextPage()
RQ-->>P: Appends next page
else No next page or fetch in progress
P-->>P: Do nothing
end
note over P: useEffect deps: inView, hasNextPage, isFetchingNextPage, fetchNextPage
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
Warning Review ran into problems🔥 ProblemsGit: Failed to clone repository. Please run the ✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
examples/react/load-more-infinite-scroll/src/pages/index.tsx (1)
52-56: Nit: prefer early-return and explicitly ignore the Promise.Slightly flattens the effect and avoids any no-floating-promises lint:
- React.useEffect(() => { - if (inView && hasNextPage && !isFetchingNextPage) { - fetchNextPage() - } - }, [inView, hasNextPage, isFetchingNextPage, fetchNextPage]) + React.useEffect(() => { + if (!inView || !hasNextPage || isFetchingNextPage) return + void fetchNextPage() + }, [inView, hasNextPage, isFetchingNextPage, fetchNextPage])
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
examples/react/load-more-infinite-scroll/src/pages/index.tsx(1 hunks)
🔇 Additional comments (1)
examples/react/load-more-infinite-scroll/src/pages/index.tsx (1)
53-56: Good fix: guard fetchNextPage with hasNextPage and !isFetchingNextPage.Prevents duplicate/in-flight fetches and aligns with recommended usage. Looks correct with the expanded deps array.
In the infinite scroll example, I added
hasNextPage && !isFetchingNextPageguards to theuseEffectthat triggersfetchNextPage.Without these guards, the effect may fire multiple times or after there are no more pages, causing unnecessary fetches.
This aligns the example with recommended usage from the docs of
useInfiniteQuery.Summary by CodeRabbit
Bug Fixes
Performance