Sub topic 15
When not to virtualize: pagination and content-visibility
This chapter assumes you can write React function components with useState, useRef, and useEffect. It also assumes you know basic CSS positioning and how scroll events work. You have already built a fixed-height virtual list by hand and used TanStack Virtual. Here you learn when those tools are the wrong choice, and what to use instead.
Virtualization has a cost
Virtualization removes most rows from the DOM. That saves rendering work, but it also removes things the browser normally gives you for free:
- Browser find-in-page (Ctrl+F) cannot match text in rows that are not mounted.
- Screen readers cannot see the full list unless you add attributes such as
aria-rowcountandaria-setsize. - Row state is lost when a row unmounts, unless you store it outside the row.
- Rows need absolute positioning, so normal document flow and CSS layout no longer size them.
- Dynamic heights need measuring, and wrong estimates make the scrollbar jump.
- Scroll position,
scrollIntoView, and anchor links work only for rows that exist.
You pay for these on every list you virtualize. So the question is not "can this list be virtualized?" but "does this list need it?"
Decide from measurements
You measured list performance earlier in this tutorial. Use the same numbers to decide. Check these before you add any code:
- How many rows does the list have in practice, not in the worst case?
- How long does the initial render take in the React Profiler?
- Does scrolling drop frames in the browser's Performance panel?
- How many DOM nodes does each row create?
- Does the slowness come from rendering, or from something else, such as a slow data fetch or an expensive row component?
A list of 200 simple rows rarely needs virtualization. A list of 5,000 rows with several nested elements each often does. Between those, measure on a slow device, because a fast development laptop hides problems.
Alternative 1: pagination
Pagination splits the data into pages and renders one page at a time. Each page has a bounded number of rows, so render cost stays constant no matter how large the dataset grows.
Pagination fits when:
- Users look for a specific item, not for continuous scanning.
- Users need to bookmark or share a position. A URL such as
/orders?page=3is stable. - The server can return one page at a time, which also cuts network and memory use.
- Accessibility and find-in-page matter. Every row on the current page is a real DOM node.
- The footer must be reachable. With infinite scrolling, a footer below the list can be unreachable.
Here is a complete paginated list that runs as shown:
import { useState } from "react";
type PaginatedListProps = {
items: string[];
pageSize?: number;
};
export function PaginatedList({ items, pageSize = 50 }: PaginatedListProps) {
const [page, setPage] = useState(0);
const pageCount = Math.max(1, Math.ceil(items.length / pageSize));
const start = page * pageSize;
const visible = items.slice(start, start + pageSize);
return (
<section aria-label="Results">
<ul>
{visible.map((item, i) => (
<li key={start + i}>{item}</li>
))}
</ul>
<nav aria-label="Pagination">
<button onClick={() => setPage((p) => p - 1)} disabled={page === 0}>
Previous
</button>
<span>
Page {page + 1} of {pageCount}
</span>
<button
onClick={() => setPage((p) => p + 1)}
disabled={page >= pageCount - 1}
>
Next
</button>
</nav>
</section>
);
}The trade-off is interaction cost. Users must click to move between pages, and comparing items across pages is hard. Pagination also does not suit feeds where people expect to keep scrolling.
You can combine approaches. A paginated table with 500 rows per page might still use virtualization inside the page. Pick the page size from your measurements.
Alternative 2: CSS content-visibility
The content-visibility CSS property lets the browser skip layout and paint work for elements that are off-screen. The elements stay in the DOM. This is the key difference from virtualization.
.row {
content-visibility: auto;
contain-intrinsic-size: auto 40px;
}With content-visibility: auto, the browser skips rendering the row's contents while the row is outside the viewport (plus a margin the browser chooses). It renders them when the row nears the viewport.
The contain-intrinsic-size property gives skipped rows a placeholder size. Without it, a skipped row collapses to zero height, and the scrollbar would be wrong. The auto keyword tells the browser to remember the real size after the row has rendered once. Until then, it uses 40px.
What you keep
Because rows stay in the DOM:
- Find-in-page still works, and the browser scrolls to a match.
- Screen readers can still reach the content, since skipped content stays in the accessibility tree.
- Row state stays alive. React does not unmount anything.
- Normal flow layout still works, so you need no absolute positioning.
- Anchor links and
scrollIntoViewwork for every row.
What you do not get
content-visibility skips browser work only: layout and paint. React still creates every element during the first render. If the cost is in React's render phase or in the number of DOM nodes, the property will not help much. The first render of 20,000 rows still runs all 20,000 row components and creates all their nodes.
| Concern | Virtualization | content-visibility | Pagination |
|---|---|---|---|
| React render cost | Low | Unchanged | Low |
| DOM node count | Low | Unchanged | Low |
| Layout and paint cost | Low | Low | Low |
| Find-in-page | Broken for hidden rows | Works | Current page only |
| Row state kept | Needs extra work | Yes | Lost when the page changes |
| Implementation effort | High | Very low | Medium |
| Works with variable heights | Needs measuring | Yes | Yes |
Edge cases
- Browser support: check current support before relying on it. Browsers that lack the property ignore it, so the list still works and is only slower. That makes it a safe progressive enhancement.
- Scrollbar jitter: the first time a row renders, its real height can differ from the placeholder. The scrollbar thumb may shift. A good
contain-intrinsic-sizeestimate reduces this. - Do not apply it to elements that must always be measured, such as a row you read with
getBoundingClientRecton mount. Skipped content may report sizes based on the placeholder. - Use it on the row, not on the scroll container. Applying it to the container skips the whole list.
Choosing between the three
Work from the cheapest option up:
- Do nothing if measurements show the list is fast enough.
- Fix expensive row components with memoization.
- Add
content-visibility: autoto rows. It takes two lines of CSS and little risk. - Paginate if the product allows it.
- Virtualize when you need continuous scrolling over thousands of rows and the cheaper options are not enough.
Common errors
- Virtualizing by habit. A 100-row list with a virtualization library adds code, bugs, and accessibility work for no visible gain.
- Using
content-visibilitywithoutcontain-intrinsic-size. Skipped rows collapse, the scrollbar is wrong, and many rows can become "visible" at once, which defeats the purpose. - Expecting
content-visibilityto cut React render time. It does not. Profile again after adding it, and compare the React commit time and the browser's rendering time separately. - Pagination with unstable keys. Use a stable identifier such as an item id as the key when one exists. Index keys are acceptable only for static lists, as in the example above.
- Skipping the measurement after the change. Always compare before and after, using the same data and device.
Exercise
A page shows 3,000 order rows in a plain list. Profiling shows the React commit takes 40 ms, but layout and paint take 900 ms. Users often press Ctrl+F to find an order number. Which option should you try first, and why?
Show answer
Try content-visibility: auto with contain-intrinsic-size on each row. The cost is in browser layout and paint, which this property skips for off-screen rows. React's 40 ms commit is already cheap, so reducing React work would not help. The property also keeps every row in the DOM, so Ctrl+F still finds order numbers, which virtualization would break. Measure again afterward, and move to pagination or virtualization only if the result is still too slow.
Exercise
Why can content-visibility: auto not fix a list whose slowness comes from React rendering 20,000 row components on first load?
Show answer
The property only lets the browser skip layout and paint for off-screen elements. React still runs every row component and creates every DOM node during the first render. To reduce that cost you must render fewer rows, using pagination or virtualization.