0Pricing
React Academy · Lesson

Row Selection and Virtualized Tables

Add checkbox row selection and combine TanStack Table with TanStack Virtual for large dataset rendering.

Row Selection and Virtualized Tables is a free React Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Enabling Row Selection

Add a selection column to your ColumnDef array using the id 'select'. The cell renders a checkbox wired to row.getToggleSelectedHandler(). The header renders a select-all checkbox using table.getToggleAllRowsSelectedHandler().

Also pass rowSelection state and onRowSelectionChange to useReactTable.

Checking Selection State

row.getIsSelected() returns true when a row is currently selected, perfect for applying a highlighted background style to selected rows.

row.getIsSomeSelected() handles the indeterminate checkbox state when only some rows in a group are selected (useful for grouped tables).

Getting Selected Rows

table.getSelectedRowModel().rows returns an array of all currently selected row objects. Each row has a row.original property containing the original data object.

This is how you build bulk action buttons that operate on the selected set, like "Delete selected" or "Export selected".

Bulk Action UI Pattern

Conditionally render action buttons based on table.getSelectedRowModel().rows.length > 0. When no rows are selected, hide or disable the bulk action toolbar to avoid confusing empty-state interactions.

Display the selected count in the UI: "{count} rows selected" keeps users informed of their selection.

Why Virtualize Tables?

Rendering 10,000 rows as DOM nodes is extremely slow. The browser must layout, paint, and maintain thousands of elements. Users experience janky scrolling and slow initial renders.

Virtualization renders only the rows currently visible in the viewport, dramatically reducing DOM size and improving performance.

Installing TanStack Virtual

Install @tanstack/react-virtual alongside @tanstack/react-table. The two libraries are separate but designed to complement each other. TanStack Table manages your data logic; TanStack Virtual manages which items are rendered in the DOM.

The integration point is the row array from table.getRowModel().rows.

useVirtualizer Setup

Call useVirtualizer({ count: table.getRowModel().rows.length, getScrollElement: () => tableContainerRef.current, estimateSize: () => 35 }). The count drives how many virtual items exist, matching the table's current row count after all filters.

estimateSize provides an initial row height estimate; actual heights are measured as rows render.

Rendering Virtual Items

Call virtualizer.getVirtualItems() to get only the rows that should currently be in the DOM. Map over this array, access the real row via table.getRowModel().rows[virtualRow.index], and render only those tr elements.

Each virtual item has a start property for absolute positioning within the scroll container.

Padding Spacer Divs

To maintain correct scroll position, add a top spacer div with height equal to the first virtual item's start, and a bottom spacer div with height equal to the total size minus the last item's end.

These invisible divs fill the space that un-rendered rows would occupy, keeping the scrollbar proportionally correct.

Dynamic Row Height with measureElement

Pass measureElement: el => el.getBoundingClientRect().height to useVirtualizer for accurate dynamic row heights. Each row ref is attached to the tr element so the virtualizer measures its actual rendered height.

This handles rows with varying content (multi-line text, expanded rows) without layout shifts.

Combining All Features

TanStack Table with sorting, filtering, pagination, row selection, and TanStack Virtual all compose together. The row count passed to useVirtualizer should come from table.getRowModel().rows after all active row models have processed the data.

With pagination enabled, you paginate first, then virtualize the current page's rows for smooth scrolling within a large page.

TanStack Table Row Selection

Which method returns the original data objects for all currently selected rows?

Lesson Recap

Row selection uses a checkbox column with getToggleSelectedHandler, rowSelection state, and table.getSelectedRowModel().rows to access selected data. Virtualization with useVirtualizer renders only visible rows using getVirtualItems, spacer divs, and optional measureElement for dynamic heights.

All TanStack Table features (sort, filter, pagination, selection) compose cleanly with virtual rendering.

Frequently asked questions

Is the “Row Selection and Virtualized Tables” lesson free?

Yes — the full text of “Row Selection and Virtualized Tables” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.

What will I learn in “Row Selection and Virtualized Tables”?

Add checkbox row selection and combine TanStack Table with TanStack Virtual for large dataset rendering. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start React Academy?

No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Row Selection and Virtualized Tables” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this React Academy lesson?

Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. TanStack Table Philosophy and Headless Design
  2. Defining Columns and Rendering Rows
  3. Sorting, Filtering, and Pagination
  4. Row Selection and Virtualized Tables
← Back to React Academy