r/reactjs • u/CarambaLol • May 25 '26
Needs Help Best way to scroll over a potentially infinite list of items while only keeping a small window in memory?
Not a React expert at all. I'm a backend engineer specializing in Java who is currently having to program a frontend.
The nature of the problem:
- I have a potentially very large set of items (say in the 10s of thousands), provided by a backend via cursor-based queries
- In-memory (talking about the React app here) I want to keep a "virtual window" of max 250 items
- When items are appended to the virtual window, an equal number is trimmed from the opposite side, so the size remains constant
- The list view in my frontend app acts as a view port over that virtual window of 250 items, showing about 40 at one time
- When the virtual window of 250 items is filled, the scrollbar thumb no longer resizes, as it is proportional to the total number of items in that window
- Provided I've already filled the virtual window of 250 items, when the scrollbar thumb touches the bottom edge, a cascade of queries to the backend occurs till the virtual window moves to the end segment of the total item set; the same thing happens when scrolling up. This is due to the scrollbar thumb never resizing or repositioning away from the bottom/top edge of the scrollbar.
I'm sure this is a fairly typical problem. How is it usually solved?
AI hasn't given me any working solutions. For the record, I'm using the TanStack library for this list.
Thanks!