The tea shop's order history has 10,000 rows, and about ten fit in its scroll box at once. A virtual list renders only those, plus a few on each side, inside a spacer as tall as the whole list. Write visibleRange({ scrollTop, viewportHeight, rowHeight, rowCount, overscan }), the math that picks those rows. Every row is rowHeight pixels tall, the box shows viewportHeight pixels of the list from scrollTop down, and overscan is 3 when it isn't given. The React problem Virtualized list (virtualized-list) builds the whole component; this is the counting at its heart.
Return { start, end, offsetTop, totalHeight }. Row i covers the pixels from i * rowHeight up to, but not including, (i + 1) * rowHeight, and the box shows the pixels from scrollTop up to, but not including, scrollTop + viewportHeight. A row is in view when it shares at least one pixel with the box, so a row that starts exactly at the box's bottom edge isn't. start is the first row in view minus overscan, never below 0, and end is the last row in view plus overscan, never past rowCount - 1. offsetTop is where row start begins, start * rowHeight, and totalHeight is the spacer's height, rowCount * rowHeight.
First clamp scrollTop to the positions the box can really scroll to: from 0 to totalHeight - viewportHeight, or just 0 when the list is shorter than the box. Elastic scrolling, as on iOS, can report a position below 0 or past the end for a moment while a list bounces, and the rows you return must still exist. scrollTop can have a fraction, on a zoomed page or a high-density screen. An empty list renders nothing: return { start: 0, end: -1, offsetTop: 0, totalHeight: 0 }, so a loop from start to end runs no times.
visibleRange({ scrollTop: 4500, viewportHeight: 300, rowHeight: 30, rowCount: 10000, overscan: 2 });
// Rows 150 to 159 are in view, so with 2 more on each side:
// { start: 148, end: 161, offsetTop: 4440, totalHeight: 300000 }
expect(visibleRange({ scrollTop: 0, viewportHeight: 300, rowHeight: 30, rowCount: 10000, overscan: 0 })).toEqual({ start: 0, end: 9, offsetTop: 0, totalHeight: 300000 });
const orders = { viewportHeight: 300, rowHeight: 30, rowCount: 10000, overscan: 0 };
expect(visibleRange({ ...orders, scrollTop: 45 })).toEqual({ start: 1, end: 11, offsetTop: 30, totalHeight: 300000 });
expect(visibleRange({ ...orders, scrollTop: 60 })).toEqual({ start: 2, end: 11, offsetTop: 60, totalHeight: 300000 });
expect(visibleRange({ scrollTop: 4500, viewportHeight: 300, rowHeight: 30, rowCount: 10000, overscan: 2 })).toEqual({ start: 148, end: 161, offsetTop: 4440, totalHeight: 300000 });
const orders = { viewportHeight: 300, rowHeight: 30, rowCount: 10000 };
expect(visibleRange({ ...orders, scrollTop: 0 })).toEqual({ start: 0, end: 12, offsetTop: 0, totalHeight: 300000 });
expect(visibleRange({ ...orders, scrollTop: 299700 })).toEqual({ start: 9987, end: 9999, offsetTop: 299610, totalHeight: 300000 });
const orders = { viewportHeight: 300, rowHeight: 30, rowCount: 10000, overscan: 0 };
expect(visibleRange({ ...orders, scrollTop: -40 })).toEqual({ start: 0, end: 9, offsetTop: 0, totalHeight: 300000 });
expect(visibleRange({ ...orders, scrollTop: 400000 })).toEqual({ start: 9990, end: 9999, offsetTop: 299700, totalHeight: 300000 });
expect(visibleRange({ scrollTop: 20, viewportHeight: 300, rowHeight: 30, rowCount: 5, overscan: 2 })).toEqual({ start: 0, end: 4, offsetTop: 0, totalHeight: 150 });
expect(visibleRange({ scrollTop: 0, viewportHeight: 300, rowHeight: 30, rowCount: 0 })).toEqual({ start: 0, end: -1, offsetTop: 0, totalHeight: 0 });
const orders = { viewportHeight: 300, rowHeight: 30, rowCount: 10000, overscan: 0 };
expect(visibleRange({ ...orders, scrollTop: 59.5 })).toEqual({ start: 1, end: 11, offsetTop: 30, totalHeight: 300000 });
expect(visibleRange({ ...orders, scrollTop: 60.5 })).toEqual({ start: 2, end: 12, offsetTop: 60, totalHeight: 300000 });
Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.
Ctrl + Enter runs the tests (⌘ + Enter on a Mac).
I'm here to help with Visible rows in a virtual list. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.