Skip to article content
FrontendXLesson 29 / 40

List Virtualization and Large Data

Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.

advanced · about 55 minutes · 4 exercises

  • Predict
  • Implement
  • Debug
  • Interview

What you will be able to do

#
  • Explain windowing and why it keeps the DOM small.
  • Sketch a fixed-height virtual list with overscan.
  • State the accessibility trade-offs of virtualization.

Overview

#

Rendering too many rows is the cost

A list of thousands of rows creates thousands of DOM nodes. The browser spends time on layout and paint, and React spends time creating elements. Virtualization, also called windowing, renders only the rows in or near the viewport.

How windowing works

You track the scroll position and the viewport height, compute which rows are visible, and render only those, offsetting them with a spacer so the scrollbar reflects the full list.

snippet.tsx
const start = Math.floor(scrollTop / rowHeight);
const visibleCount = Math.ceil(viewportHeight / rowHeight);
const rows = items.slice(start, start + visibleCount + overscan);

Overscan

Rendering a few extra rows above and below the viewport, the overscan, hides the blank flash during fast scrolling. Too much overscan defeats the purpose.

Fixed versus variable size

Fixed row heights make the math trivial. Variable heights require measuring rendered rows and caching the measurements, which is more complex and can shift the scroll position.

Keys still matter

Windowing reuses DOM nodes as rows enter and leave. Use stable keys so React matches the right data to the right node and does not carry state between rows.

Pitfall: accessibility and find-in-page

Windowed content is not in the DOM, so browser find-in-page and some assistive technology may miss off-screen rows. Provide a way to reach all data, such as a search, and do not virtualize short lists.

When to virtualize

Virtualize long, uniform, scrollable lists. Do not virtualize short lists, tables with complex row spanning, or content that must be fully present for search or printing.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    A list of 10000 rows is virtualized with a viewport showing 20 rows. How many rows are rendered?

    Read the code, then choose the best answer. The reference explanation stays hidden until you respond.
    stimulus.tsx
    const rows = items.slice(start, start + visibleCount + overscan);
    Choose the best answer
  2. Exercise 2Implement

    Sketch a fixed-height virtual list that renders only the visible rows plus overscan.

    starter.tsx
    export function VirtualList({ items, rowHeight, height }: { items: Item[]; rowHeight: number; height: number }) {
      return null;
    }

    Reveal the reference answer
    reference.tsx
    import { useState } from 'react';
    
    export function VirtualList({ items, rowHeight, height }: { items: Item[]; rowHeight: number; height: number }) {
      const [scrollTop, setScrollTop] = useState(0);
      const overscan = 5;
      const start = Math.max(0, Math.floor(scrollTop / rowHeight) - overscan);
      const visible = Math.ceil(height / rowHeight) + overscan * 2;
      const slice = items.slice(start, start + visible);
      return (
        <div style={{ height, overflowY: 'auto' }} onScroll={(e) => setScrollTop(e.currentTarget.scrollTop)}>
          <div style={{ height: items.length * rowHeight, position: 'relative' }}>
            {slice.map((item, i) => (
              <div key={item.id} style={{ position: 'absolute', top: (start + i) * rowHeight, height: rowHeight }}>
                {item.label}
              </div>
            ))}
          </div>
        </div>
      );
    }

    Authored expected output: Only the visible rows plus overscan are in the DOM; the scrollbar reflects the full list.

    The outer container scrolls and the inner spacer sets the full height. Rows are absolutely positioned at their real offset, so the scrollbar is correct while only a slice is rendered.

  3. Exercise 3Debug

    This virtual list shows the wrong row content after scrolling. Explain and fix.

    starter.tsx
    slice.map((item, i) => <div key={i}>{item.label}</div>)

    Reveal the reference answer
    reference.tsx
    slice.map((item) => <div key={item.id}>{item.label}</div>)

    Authored expected output: Each visible row shows its own data after scrolling.

    Using the slice index as the key means the same key maps to different items as the window moves, so React reuses the wrong node. A stable item.id key keeps identity correct.

  4. Exercise 4Interview

    A table of 50000 rows is slow. Explain your approach and the trade-offs of virtualization.

    starter.tsx
    // large table, scrolling, find-in-page

    Reveal the reference answer
    reference.tsx
    Profile first to confirm that rendering the rows is the cost. Then virtualize the rows so only the visible window plus overscan is in the DOM, which keeps layout and paint cheap. Use fixed row heights if possible; variable heights need measurement and can shift scroll. Keep stable keys so reused nodes match the right data. The trade-offs are that off-screen content is not in the DOM, so find-in-page and some assistive technology miss it, and complex row spans are hard to virtualize. Mitigate with search and by not virtualizing short lists.

    Authored expected output: A measured plan plus the accessibility and complexity trade-offs.

    The answer should pair the performance fix with its costs, since virtualization trades DOM completeness for speed.

Common misconceptions

#
  • Virtualization makes the data smaller. It reduces rendered DOM nodes, not the data.
  • Any list should be virtualized. Short lists and content that must be fully searchable should not be.
  • Overscan should be large to be smooth. Excessive overscan renders too much and loses the benefit.
  • Keys do not matter with windowing. Windowed lists reuse nodes, so stable keys are essential.

Follow-up practice

#
  • react-profiling-and-devtools
  • react-memoization-strategies

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.