The DataGrid component is designed to handle large datasets efficiently while offering a rich set of features for customization and interactivity.
Important
rolldown-viteby default useslightningcssto minify css which has a bug minifying light-dark syntax. You can switch toesbuildas a workaround
build: {
....,
cssMinify: 'esbuild'
}
Install react-data-grid using your favorite package manager:
npm i react-data-grid
pnpm add react-data-grid
yarn add react-data-grid
bun add react-data-grid
Additionally, import the default styles in your application:
import 'react-data-grid/lib/styles.css';
react-data-grid is published as ECMAScript modules for evergreen browsers, bundlers, and server-side rendering.
Here is a basic example of how to use react-data-grid in your React application:
import 'react-data-grid/lib/styles.css';
import { DataGrid, type Column } from 'react-data-grid';
interface Row {
id: number;
title: string;
}
const columns: readonly Column<Row>[] = [
{ key: 'id', name: 'ID' },
{ key: 'title', name: 'Title' }
];
const rows: readonly Row[] = [
{ id: 0, title: 'Example' },
{ id: 1, title: 'Demo' }
];
function App() {
return <DataGrid columns={columns} rows={rows} />;
}
Set --rdg-color-scheme: light/dark at the :root to control the color theme. The light or dark themes can be enforced using the rdg-light or rdg-dark classes.
<DataGrid />columns: readonly Column<R, SR>[]An array of column definitions. Each column should have a key and name. See the Column type for all available options.
:warning: Performance: Passing a new columns array will trigger a re-render and recalculation for the entire grid. Always memoize this prop using useMemo or define it outside the component to avoid unnecessary re-renders.
rows: readonly R[]An array of rows, the rows data can be of any type.
:bulb: Performance: The grid is optimized for efficient rendering:
setRows([...rows])) triggers viewport and layout recalculations, even if the row objects are unchanged```tsx // ✅ Good: Only changed row is re-rendered setRows(rows.map((row, idx) => (idx === targetIdx ? { ...row, updated: true } : row)));
// ❌ Avoid: Creates new references for all rows, causing all visible rows to re-render setRows(rows.map((row) => ({ ...row }))); ```
topSummaryRows?: Maybe<readonly SR[]>Rows pinned at the top of the grid for summary purposes.
bottomSummaryRows?: Maybe<readonly SR[]>Rows pinned at the bottom of the grid for summary purposes.
rowKeyGetter?: Maybe<(row: R) => K>Function to return a unique key/identifier for each row. rowKeyGetter is required for row selection to work.
import { DataGrid } from 'react-data-grid';
interface Row {
id: number;
name: string;
}
function rowKeyGetter(row: Row) {
return row.id;
}
function MyGrid() {
return <DataGrid columns={columns} rows={rows} rowKeyGetter={rowKeyGetter} />;
}
:bulb: While optional, setting this prop is recommended for optimal performance as the returned value is used to set the key prop on the row elements.
:warning: Performance: Define this function outside your component or memoize it with useCallback to prevent unnecessary re-renders.
onRowsChange?: Maybe<(rows: R[], data: RowsChangeData<R, SR>) => void>Callback triggered when rows are changed.
The first parameter is a new rows array with both the updated rows and the other untouched rows.
The second parameter is an object with an indexes array highlighting which rows have changed by their index, and the column where the change happened.
import { useState } from 'react';
import { DataGrid } from 'react-data-grid';
function MyGrid() {
const [rows, setRows] = useState(initialRows);
return <DataGrid columns={columns} rows={rows} onRowsChange={setRows} />;
}
rowHeight?: Maybe<number | ((row: R) => number)>Default: 35 pixels
Height of each row in pixels. A function can be used to set different row heights.
:warning: Performance: When using a function, the height of all rows is calculated upfront on every render. For large datasets (1000+ rows), this can cause performance issues. Consider using a fixed height when possible, or memoize the rowHeight function.
headerRowHeight?: Maybe<number>Default: 35 pixels
Height of the header row in pixels.
summaryRowHeight?: Maybe<number>Default: 35 pixels
Height of each summary row in pixels.
columnWidths?: Maybe<ColumnWidths>A map of column widths containing both measured and resized widths. If not provided then an internal state is used.
const [columnWidths, setColumnWidths] = useState((): ColumnWidths => new Map());
function addNewRow() {
setRows(...);
// reset column widths after adding a new row
setColumnWidths(new Map());
}
return <DataGrid columnWidths={columnWidths} onColumnWidthsChange={setColumnWidths} ... />
onColumnWidthsChange?: Maybe<(columnWidths: ColumnWidths) => void>Callback triggered when column widths change. If not provided then an internal state is used.
selectedRows?: Maybe<ReadonlySet<K>>A set of selected row keys. rowKeyGetter is required for row selection to work.
isRowSelectionDisabled?: Maybe<(row: NoInfer<R>) => boolean>Function to determine if row selection is disabled for a specific row.
onSelectedRowsChange?: Maybe<(selectedRows: Set<K>) => void>Callback triggered when the selection changes.
import { useState } from 'react';
import { DataGrid, SelectColumn } from 'react-data-grid';
const rows: readonly Rows[] = [...];
const columns: readonly Column<Row>[] = [
SelectColumn,
// other columns
];
function rowKeyGetter(row: Row) {
return row.id;
}
function isRowSelectionDisabled(row: Row) {
return !row.isActive;
}
function MyGrid() {
const [selectedRows, setSelectedRows] = useState((): ReadonlySet<number> => new Set());
return (
<DataGrid
rowKeyGetter={rowKeyGetter}
columns={columns}
rows={rows}
selectedRows={selectedRows}
isRowSelectionDisabled={isRowSelectionDisabled}
onSelectedRowsChange={setSelectedRows}
/>
);
}
sortColumns?: Maybe<readonly SortColumn[]>An array of sorted columns.
onSortColumnsChange?: Maybe<(sortColumns: SortColumn[]) => void>Callback triggered when sorting changes.
import { useState } from 'react';
import { DataGrid, SelectColumn } from 'react-data-grid';
const rows: readonly Rows[] = [...];
const columns: readonly Column<Row>[] = [
{
key: 'name',
name: 'Name',
sortable: true
},
// other columns
];
function MyGrid() {
const [sortColumns, setSortColumns] = useState<readonly SortColumn[]>([]);
return (
<DataGrid
columns={columns}
rows={rows}
sortColumns={sortColumns}
onSortColumnsChange={setSortColumns}
/>
);
}
More than one column can be sorted via ctrl (command) + click. To disable multiple column sorting, change the onSortColumnsChange function to
function onSortColumnsChange(sortColumns: SortColumn[]) {
setSortColumns(sortColumns.slice(-1));
}
defaultColumnOptions?: Maybe<DefaultColumnOptions<R, SR>>Default options applied to all columns.
function MyGrid() {
return (
<DataGrid
columns={columns}
rows={rows}
defaultColumnOptions={{
minWidth: 100,
resizable: true,
sortable: true,
draggable: true
}}
/>
);
}
onFill?: Maybe<(event: FillEvent<R>) => R>onCellMouseDown: Maybe<(args: CellMouseArgs<R, SR>, event: CellMouseEvent) => void>Callback triggered when a pointer becomes active in a cell. The default behavior is to select the cell. Call preventGridDefault to prevent the default behavior.
function onCellMouseDown(args: CellMouseDownArgs<R, SR>, event: CellMouseEvent) {
if (args.column.key === 'id') {
event.preventGridDefault();
}
}
<DataGrid rows={rows} columns={columns} onCellMouseDown={onCellMouseDown} />;
onCellClick?: Maybe<(args: CellMouseArgs<R, SR>, event: CellMouseEvent) => void>Callback triggered when a cell is clicked.
function onCellClick(args: CellMouseArgs<R, SR>, event: CellMouseEvent) {
if (args.column.key === 'id') {
event.preventGridDefault();
}
}
<DataGrid rows={rows} columns={columns} onCellClick={onCellClick} />;
This event can be used to open cell editor on single click
function onCellClick(args: CellMouseArgs<R, SR>, event: CellMouseEvent) {
if (args.column.key === 'id') {
args.selectCell(true);
}
}
onCellDoubleClick?: Maybe<(args: CellMouseArgs<R, SR>, event: CellMouseEvent) => void>Callback triggered when a cell is double-clicked. The default behavior is to open the editor if the cell is editable. Call preventGridDefault to prevent the default behavior.
function onCellDoubleClick(args: CellMouseArgs<R, SR>, event: CellMouseEvent) {
if (args.column.key === 'id') {
event.preventGridDefault();
}
}
<DataGrid rows={rows} columns={columns} onCellDoubleClick={onCellDoubleClick} />;
onCellContextMenu?: Maybe<(args: CellMouseArgs<R, SR>, event: CellMouseEvent) => void>Callback triggered when a cell is right-clicked.
function onCellContextMenu(args: CellMouseArgs<R, SR>, event: CellMouseEvent) {
if (args.column.key === 'id') {
event.preventDefault();
// open custom context menu
}
}
<DataGrid rows={rows} columns={columns} onCellContextMenu={onCellContextMenu} />;
onCellKeyDown?: Maybe<(args: CellKeyDownArgs<R, SR>, event: CellKeyboardEvent) => void>A function called when keydown event is triggered on a cell. This event can be used to customize cell navigation and editing behavior.
Examples
Enterfunction onCellKeyDown(args: CellKeyDownArgs<R, SR>, event: CellKeyboardEvent) {
if (args.mode === 'SELECT' && event.key === 'Enter') {
event.preventGridDefault();
}
}
Tabfunction onCellKeyDown(args: CellKeyDownArgs<R, SR>, event: CellKeyboardEvent) {
if (args.mode === 'SELECT' && event.key === 'Tab') {
event.preventGridDefault();
}
}
Check more examples
$ claude mcp add react-data-grid \
-- python -m otcore.mcp_server <graph>