Each row in a list is cheap. Ten thousand of them aren't. This lab shows 10,000 logged study sessions in a scrolling box that's 256 pixels tall: room for 8 rows.
npm install and npm run dev. Scroll the list, then untick Windowed list, and watch the console.
What you'll see
Windowed, the list mounts in a few milliseconds, and the page holds about a dozen rows however far you scroll. Unticked, React creates all 10,000 rows even though only 8 are visible, so List update takes longer. Record both durations on your device; exact numbers vary with hardware, browser and development mode. Compare the two versions rather than trying to reproduce someone else's time.
Windowing
WindowedList renders only the rows in view, plus a few either side so fast
scrolling doesn't show gaps:
const first = Math.max(0, Math.floor(scrollTop / rowHeight) - overscan);
const last = Math.min(items.length, Math.ceil((scrollTop + height) / rowHeight) + overscan);- An inner element has the full height,
items.length * rowHeight, so the scrollbar is the right size and scrolling feels real. - Each visible row is positioned absolutely at
index * rowHeight. - Scrolling updates
scrollTop, and the window moves:
return (
<div style={{ height, overflowY: "auto" }} onScroll={(e) => setScrollTop(e.currentTarget.scrollTop)}>
<ul className="cards" style={{ height: items.length * rowHeight, position: "relative", gap: 0 }}>
{visible.map((item, i) => (
<li key={item.id} style={{ position: "absolute", top: (first + i) * rowHeight, height: rowHeight, left: 0, right: 0 }}>
{renderRow(item)}
</li>
))}
</ul>
</div>
);The number of mounted rows now depends on the box's height, not the total number of items. Keeping, filtering and sorting the full data still costs time and memory, and browsers have limits on scrollable dimensions. Windowing makes rendering fewer rows cheaper; it doesn't make a huge data set free.
The price
- Rows need a known height. Variable heights need measuring, which is harder; libraries such as TanStack Virtual handle it.
- The browser's find-in-page can't see rows that aren't rendered, and neither can a screen reader's list navigation. For lists people search with the browser, consider pagination or "Load more" instead.
- It's code to maintain. Below a few hundred simple rows, plain rendering is usually fine. Measure first.
Jump to a session
Add a Go to session number box and a Go button above the list. Clicking Go with 5000 scrolls the windowed list so that session #5000 is the top row:
#5000 Context · 53 minDo it without rendering more rows: the page should still hold about a dozen. Disable Go when the list isn't windowed.
npm install and npm run dev. Hint 1: the idea
Row 5000 isn't on the page, so there's no element to call scrollIntoView on:
this is the find-in-page problem again. But you know exactly where it would
be, from the same arithmetic WindowedList uses to place rows. Setting the
box's scroll position fires its onScroll, and the window moves there by
itself. The box belongs to WindowedList, so it should offer the parent a
small way to ask for that, like Day 1's timer handle.
Hint 2: a first step
In WindowedList, give the scrolling <div> a ref, and accept a ref prop.
Expose one method with useImperativeHandle, scrollToIndex(index), that only
logs the index for now. In App, pass a ref to WindowedList and call the
method from Go: the console should show 4999 for session 5000, because
ids start at 1 and indexes at 0. Then make the method set the box's
scrollTop. What position does row 4999 start at?
Show one solution
In WindowedList.jsx:
import { useImperativeHandle, useRef, useState } from "react";
export default function WindowedList({ items, rowHeight, height, renderRow, overscan = 4, ref }) {
const [scrollTop, setScrollTop] = useState(0);
const box = useRef(null);
useImperativeHandle(ref, () => ({
scrollToIndex(index) {
box.current.scrollTop = index * rowHeight;
},
}), [rowHeight]);
// ...first, last and visible as before
}and ref={box} on the scrolling <div>. In App.jsx:
const listRef = useRef(null);
const [target, setTarget] = useState("5000");<p>
<label>
Go to session <input type="number" min={1} max={sessions.length} value={target} onChange={(e) => setTarget(e.target.value)} />
</label>{" "}
<button onClick={() => listRef.current.scrollToIndex(Number(target) - 1)} disabled={!windowed}>Go</button>
</p><WindowedList ref={listRef} items={sessions} rowHeight={ROW} height={256} renderRow={(s) => <Row session={s} />} />Import useRef in App.jsx. Row 4999 starts at 4999 × 32 = 159,968 pixels, so
that's the scroll position. The browser fires scroll, WindowedList stores
the new scrollTop, and the next render mounts sessions #4996 to #5011.
The parent never touches the box or the row arithmetic: it only asks for an
index.
Completion is saved in this browser only. It does not measure mastery.