Sub topic 5
Computing which rows are visible
This chapter assumes you know React state and props, basic CSS positioning, and how the scroll event reports scrollTop. The previous chapter set up a scroll container with a tall spacer inside it. This chapter works out which rows to render inside that container.
The inputs
Three numbers decide the visible range:
scrollTopis how many pixels the container has scrolled. It is 0 at the top.heightis the pixel height of the container's visible area, called the viewport.rowHeightis the fixed height of one row.
Row i occupies the pixels from i * rowHeight to (i + 1) * rowHeight. The viewport shows the pixels from scrollTop to scrollTop + height. Dividing those two edges by rowHeight turns pixels into row indexes.
The formulas
- The first visible row is
Math.floor(scrollTop / rowHeight). Rounding down includes a row that is only partly visible at the top. - The last visible row is
Math.ceil((scrollTop + height) / rowHeight) - 1. Rounding up includes a partly visible row at the bottom. Subtracting 1 converts a count of rows into an index.
A worked example
Take rowHeight = 40, height = 400, and scrollTop = 1000.
- First visible:
floor(1000 / 40)is 25. - Bottom edge:
1000 + 400 = 1400, andceil(1400 / 40)is 35, so the last visible row is 34.
Rows 25 to 34 are on screen, which is 10 rows. This holds whether the list has 100 rows or 1,000,000. With an overscan of 3, the rendered range is 22 to 37.
As a pure function
Keep this calculation in a plain function with no React in it. You can test it on its own, and a component can call it on every render.
export type Range = { first: number; last: number };
export function computeRange(
scrollTop: number,
viewportHeight: number,
rowHeight: number,
count: number,
overscan = 3
): Range {
if (count === 0) return { first: 0, last: -1 };
const top = Math.max(0, scrollTop);
const firstVisible = Math.floor(top / rowHeight);
const lastVisible = Math.ceil((top + viewportHeight) / rowHeight) - 1;
return {
first: Math.max(0, firstVisible - overscan),
last: Math.min(count - 1, lastVisible + overscan),
};
}
console.log(computeRange(1000, 400, 40, 10000, 0)); // { first: 25, last: 34 }
console.log(computeRange(1000, 400, 40, 10000, 3)); // { first: 22, last: 37 }A loop such as for (let i = first; i <= last; i++) then renders each row at top: i * rowHeight.
Edge cases
- Empty list. Returning
last: -1makes the loop run zero times, so no special case is needed in the component. - Negative
scrollTop. Safari's elastic overscroll can report values below 0. TheMath.max(0, scrollTop)clamp keeps the indexes valid. - List shrinks while scrolled. If the data changes from 10,000 items to 50, a stale
scrollTopcan pushfirstpastlast. The loop then renders nothing. The browser usually clampsscrollTopand fires another scroll event, but do not rely on that for state you store yourself. - Fractional values. On high-density screens
scrollTopcan be fractional.floorandceilhandle this without extra code.
Common errors
- Using
roundinstead offloorandceil. Partly visible rows then pop in late and leave a visible gap at the edges. - Forgetting to clamp. An index below 0 or at or above
countreadsundefinedfrom the array and renders an empty row. - Reading
scrollTopfrom a stale variable instead of from the scroll event'scurrentTarget.
Exercise
A list has 500 rows, each 50px tall. The viewport is 300px high and scrollTop is 125. With an overscan of 0, which rows does computeRange return?
Show answer
The first visible row is floor(125 / 50) = 2. The bottom edge is 125 + 300 = 425, ceil(425 / 50) = 9, and subtracting 1 gives 8. The result is rows 2 to 8, which is 7 rows. Row 2 and row 8 are both only partly visible.