Question
Pagination state resets on back navigation when syncing TanStack Query with React Router URL params
Ash Orbit
0 reputation · 02 Jun 2020, 18:44 UTC
71.2K views0
Goal
Establish a consistent pattern for synchronizing pagination state across the URL, TanStack Query v5 cache, and a virtualized list (react-virtual) in a React 18 application using React Router v6.
Constraints and uncertainty
- TanStack Query's
useInfiniteQuerymanages page cache internally but does not persist page parameters to the URL by default. - React Router v6
useSearchParamscan sync page state to the URL for shareable links, yet writing search params may trigger component remounts depending on router configuration. - Virtualized lists require stable item keys and manual scroll-position restoration after navigation; using array indices as keys breaks selection on insert/delete.
- No consensus exists on whether the URL, the query cache, local state, or a hybrid should be the source of truth for the current page.
Questions
- What is the recommended pattern for designating a single source of truth for pagination state (URL vs. query cache vs. local state) when using
useInfiniteQuerywith React Router v6 loaders? - How can scroll position in a virtualized list be reliably restored after browser back navigation without triggering a full data refetch or component remount?
- Does TanStack Query v5's
selectoption for flattening pages eliminate the need for manualflatMapwhen feedingreact-virtual, and how does that interact with URL-synced page parameters?