- React 18.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
- 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. We recommend using Firefox as Chrome has a bug with frozen columns.
npm install react-data-grid
react-data-grid
is published as ECMAScript modules for evergreen browsers / bundlers, and CommonJS for server-side rendering / Jest.
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 describing the grid's columns.
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.
An optional array of summary rows, usually used to display total values for example. topSummaryRows
are pinned at the top of the rows view and the vertical scroll bar will not scroll these rows.
An optional array of summary rows, usually used to display total values for example. bottomSummaryRows
are pinned at the bottom of the rows view and the vertical scroll bar will not scroll these rows.
A function returning a unique key/identifier per 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.
A function receiving row updates.
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
Either a number defining the height of row in pixels, or a function returning dynamic row heights.
Default: 35
pixels
A number defining the height of the header row.
Default: 35
pixels
A number defining the height of summary rows.
A set of selected row keys. rowKeyGetter
is required for row selection to work.
A function used to disable row selection on certain rows.
A function called when row selection is changed.
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.
A function called when sorting is changed
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));
}
Column options that are applied to all the columns
function MyGrid() {
return (
<DataGrid
columns={columns}
rows={rows}
defaultColumnOptions={{
minWidth: 100,
resizable: true,
sortable: true,
draggable: true
}}
/>
);
}
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
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} />;
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
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
A function called when the grid is scrolled.
A function called when column is resized.
Default: true
This prop can be used to disable virtualization.
This prop can be used to override the internal renderers. The prop accepts an object of type
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 context providers or tweak props
import DataGrid, { RenderRowProps, Row } from 'react-data-grid';
function myRowRenderer(key: React.Key, props: RenderRowProps<Row>) {
return (
<MyContext.Provider key={key} value={123}>
<Row {...props} />
</MyContext.Provider>
);
}
function MyGrid() {
return <DataGrid columns={columns} rows={rows} renderers={{ renderRow: myRowRenderer }} />;
}
renderRow
.
A function to add a class on the row
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';
}
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 classname
custom styles
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. By default it will be displayed in the header cell
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
Sets the maximum width of a column.
Sets the maximum width of a column.
A function to add a class on the row
Render function used to render the content of cells
Render function used to render the content of header cells
Render function used to render the content of summary cells
R
,TRow
: Row typeSR
,TSummaryRow
: Summary row typeK
: Row key type