The DataGrid component is designed to handle large datasets efficiently while offering a rich set of features for customization and interactivity.
- React 19.0+ support
- Evergreen browsers and server-side rendering support
- Tree-shaking support and only one npm dependency to keep your bundles slim
- Great performance thanks to virtualization: columns and rows outside the viewport are not rendered
- Strictly typed with TypeScript
- Keyboard accessibility
- Light and dark mode support out of the box. The light or dark themes can be enforced using the
rdg-light
orrdg-dark
classes. - Frozen columns: Freeze columns to keep them visible during horizontal scrolling.
- Column resizing
- Multi-column sorting
- Click on a sortable column header to toggle between its ascending/descending sort order
- Ctrl+Click / Meta+Click to sort an additional column
- Column spanning
- Column grouping
- Row selection
- Row grouping
- Summary rows
- Dynamic row heights
- No rows fallback
- Cell formatting
- Cell editing
- Cell copy / pasting
- Cell value dragging / filling
- Customizable Renderers
- Right-to-left (RTL) support.
to install react-data-grid
, use npm or yarn:
npm install react-data-grid
# or
yarn 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 } from 'react-data-grid';
const columns = [
{ key: 'id', name: 'ID' },
{ key: 'title', name: 'Title' }
];
const rows = [
{ id: 0, title: 'Example' },
{ id: 1, title: 'Demo' }
];
function App() {
return <DataGrid columns={columns} rows={rows} />;
}
See Column
.
An array of column definitions. Each column should have a key and name.
columns
array will trigger a re-render for the whole grid, avoid changing it as much as possible for optimal performance.
An array of rows, the rows data can be of any type.
Rows pinned at the top of the grid for summary purposes.
Rows pinned at the bottom of the grid for summary purposes.
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} />;
}
đź’ˇ 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.
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} />;
}
Default: 35
pixels
Height of each row in pixels. A function can be used to set different row heights.
Default: 35
pixels
Height of the header row in pixels.
Default: 35
pixels
Height of each summary row in pixels.
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} ../>
Callback triggered when column widths change. If not provided then an internal state is used.
A set of selected row keys. rowKeyGetter
is required for row selection to work.
Function to determine if row selection is disabled for a specific row.
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 MyGrid() {
const [selectedRows, setSelectedRows] = useState((): ReadonlySet<number> => new Set());
return (
<DataGrid
rowKeyGetter={rowKeyGetter}
columns={columns}
rows={rows}
selectedRows={selectedRows}
isRowSelectionDisabled={isRowSelectionDisabled}
onSelectedRowsChange={setSelectedRows}
/>
);
}
function rowKeyGetter(row: Row) {
return row.id;
}
function isRowSelectionDisabled(row: Row) {
return !row.isActive;
}
An array of sorted columns.
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}
/>
);
}
Grid can be sorted on multiple columns using ctrl (command) + click
. To disable multiple column sorting, change the onSortColumnsChange
function to
onSortColumnsChange(sortColumns: SortColumn[]) {
setSortColumns(sortColumns.slice(-1));
}
Default options applied to all columns.
function MyGrid() {
return (
<DataGrid
columns={columns}
rows={rows}
defaultColumnOptions={{
minWidth: 100,
resizable: true,
sortable: true,
draggable: true
}}
/>
);
}
Callback triggered when a cell is clicked. The default behavior is to select the cell. Call preventGridDefault
to prevent the default behavior
function onCellClick(args: CellClickArgs<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: CellClickArgs<R, SR>, event: CellMouseEvent) {
if (args.column.key === 'id') {
event.preventGridDefault();
args.selectCell(true);
}
}
Arguments:
args: CellClickArgs<R, SR>
args.rowIdx
:number
- row index of the currently selected cellargs.row
:R
- row object of the currently selected cellargs.column
:CalculatedColumn<TRow, TSummaryRow>
- column object of the currently selected cellargs.selectCell
:(enableEditor?: boolean) => void
- function to manually select the cell and optionally passtrue
to start editing
event
extends React.MouseEvent<HTMLDivElement>
event.preventGridDefault:
:() => void
event.isGridDefaultPrevented
:boolean
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: CellClickArgs<R, SR>, event: CellMouseEvent) {
if (args.column.key === 'id') {
event.preventGridDefault();
}
}
<DataGrid rows={rows} columns={columns} onCellDoubleClick={onCellDoubleClick} />;
Callback triggered when a cell is right-clicked. The default behavior is to select the cell. Call preventGridDefault
to prevent the default behavior
function onCellContextMenu(args: CellClickArgs<R, SR>, event: CellMouseEvent) {
if (args.column.key === 'id') {
event.preventGridDefault();
}
}
<DataGrid rows={rows} columns={columns} onCellContextMenu={onCellContextMenu} />;
A function called when keydown event is triggered on a cell. This event can be used to customize cell navigation and editing behavior.
Examples
- Prevent editing on
Enter
function onCellKeyDown(args: CellKeyDownArgs<R, SR>, event: CellKeyboardEvent) {
if (args.mode === 'SELECT' && event.key === 'Enter') {
event.preventGridDefault();
}
}
- Prevent navigation on
Tab
function onCellKeyDown(args: CellKeyDownArgs<R, SR>, event: CellKeyboardEvent) {
if (args.mode === 'SELECT' && event.key === 'Tab') {
event.preventGridDefault();
}
}
Check more examples
onCellCopy?: Maybe<(args: CellCopyEvent<NoInfer<R>, NoInfer<SR>>, event: CellClipboardEvent) => void>
Callback triggered when a cell's content is copied.
onCellPaste?: Maybe<(args: CellPasteEvent<NoInfer<R>, NoInfer<SR>>, event: CellClipboardEvent) => void>
Callback triggered when content is pasted into a cell.
Triggered when the selected cell is changed.
Arguments:
args.rowIdx
:number
- row indexargs.row
:R
- row object of the currently selected cellargs.column
:CalculatedColumn<TRow, TSummaryRow>
- column object of the currently selected cell
Callback triggered when the grid is scrolled.
Callback triggered when column is resized.
Callback triggered when columns are reordered.
Default: true
This prop can be used to disable virtualization.
Custom renderers for cells, rows, and other components.
interface Renderers<TRow, TSummaryRow> {
renderCell?: Maybe<(key: Key, props: CellRendererProps<TRow, TSummaryRow>) => ReactNode>;
renderCheckbox?: Maybe<(props: RenderCheckboxProps) => ReactNode>;
renderRow?: Maybe<(key: Key, props: RenderRowProps<TRow, TSummaryRow>) => ReactNode>;
renderSortStatus?: Maybe<(props: RenderSortStatusProps) => ReactNode>;
noRowsFallback?: Maybe<ReactNode>;
}
For example, the default <Row />
component can be wrapped via the renderRow
prop to add contexts or tweak props
import { DataGrid, RenderRowProps, Row } from 'react-data-grid';
function myRowRenderer(key: React.Key, props: RenderRowProps<Row>) {
return (
<MyContext key={key} value={123}>
<Row {...props} />
</MyContext>
);
}
function MyGrid() {
return <DataGrid columns={columns} rows={rows} renderers={{ renderRow: myRowRenderer }} />;
}
renderRow
.
Function to apply custom class names to rows.
import { DataGrid } from 'react-data-grid';
function MyGrid() {
return <DataGrid columns={columns} rows={rows} rowClass={rowClass} />;
}
function rowClass(row: Row, rowIdx: number) {
return rowIdx % 2 === 0 ? 'even' : 'odd';
}
Custom class name for the header row.
This property sets the text direction of the grid, it defaults to 'ltr'
(left-to-right). Setting direction
to 'rtl'
has the following effects:
- Columns flow from right to left
- Frozen columns are pinned on the right
- Column resize handle is shown on the left edge of the column
- Scrollbar is moved to the left
Custom class name for the grid.
Custom styles for the grid.
The label of the grid. We recommend providing a label using aria-label
or aria-labelledby
The id of the element containing a label for the grid. We recommend providing a label using aria-label
or aria-labelledby
If the grid has a caption or description, aria-describedby
can be set on the grid element with a value referring to the element containing the description.
This prop can be used to add a testid for testing. We recommend using role
and name
to find the grid element
function MyGrid() {
return <DataGrid aria-label="my-grid" columns={columns} rows={rows} />;
}
function MyGridTest() {
const grid = screen.getByRole('grid', { name: 'my-grid' });
}
TreeDataGrid
is component built on top of DataGrid
to add row grouping. This implements the Treegrid pattern. At the moment TreeDataGrid
does not support onFill
and isRowSelectionDisabled
props
See renderers
See RenderRowProps
The ref
prop is supported.
See FormatterProps
The name of the column. Displayed in the header cell by default.
A unique key to distinguish each column
Default auto
Width can be any valid css grid column value. If not specified, it will be determined automatically based on grid width and specified widths of other columns.
width: 80; // pixels
width: '25%';
width: 'max-content';
width: 'minmax(100px, max-content)';
max-content
can be used to expand the column to show all the content. Note that the grid is only able to calculate column width for visible rows.
Default: 50
pixels
Minimum column width in pixels.
Maximum column width in pixels.
Class name(s) for the cell
Class name(s) for the header cell.
Class name(s) for the summary cell.
Render function to render the content of cells.
Render function to render the content of the header cell.
Render function to render the content of summary cells
Render function to render the content of edit cells. When set, the column is automatically set to be editable
Enables cell editing. If set and no editor property specified, then a textinput will be used as the cell editor.
Default: false
Determines whether column is frozen. Frozen columns are pinned on the left. At the moment we do not support pinning columns on the right.
Default: false
Enable resizing of the column
Default: false
Enable sorting of the column
Default: false
Enable dragging of the column
Default: false
Sets the column sort order to be descending instead of ascending the first time the column is sorted
Options for cell editing.
Default: false
Render the cell content in addition to the edit cell. Enable this option when the editor is rendered outside the grid, like a modal for example.
Default: true
Commit changes when clicking outside the cell.
Default: true
Close the editor when the row changes externally.
R
,TRow
: Row typeSR
,TSummaryRow
: Summary row typeK
: Row key type