Skip to content

React ​

The grid is also available as React component (React 18 or newer). It uses the same core, options, column definitions and styles as the Vue component, so all guides apply. Only the component API differs.

Framework switch

Select React in the navigation bar: all live examples of this documentation then run with the React component and show the React source code.

sh
pnpm add @datagrid/react-ui
tsx
import { useMemo, useState } from "react";
import { Datagrid, type ColumnConfig } from "@datagrid/react-ui";
import "@datagrid/react-ui/style.css";

type Person = { id: number; name: string; age: number };

export function Persons({ persons }: { persons: Person[] }) {
  const [selected, setSelected] = useState<Array<number | string>>([]);

  // define columns and options outside of the render function or memoize them
  const columns = useMemo<ColumnConfig<Person>[]>(
    () => [
      { field: "name", text: "Name", sortable: true, filterable: true },
      { field: "age", text: "Age", type: "number", sortable: true },
    ],
    []
  );

  return (
    <div style={{ height: 400, display: "flex" }}>
      <Datagrid
        columns={columns}
        rows={persons}
        options={{ rowId: (p) => p.id, checkboxSelection: true }}
        selectedItems={selected}
        onSelectedItemsChange={setSelected}
      />
    </div>
  );
}

Differences to the Vue component ​

VueReact
v-model:rowsrows + onRowsChange
v-model:selectedItemsselectedItems + onSelectedItemsChange
v-model:selectedCellsselectedCells + onSelectedCellsChange
v-model:expandedItemsexpandedItems + onExpandedItemsChange
v-model:sortedColumnssortedColumns + onSortedColumnsChange
v-model:columnFiltercolumnFilter + onColumnFilterChange
v-model:globalFilterglobalFilter + onGlobalFilterChange
v-model:columnGroupscolumnGroups + onColumnGroupsChange
v-model:pageIndexpageIndex + onPageIndexChange
@ready, @cell-click, …onReady, onCellClick, onCellDoubleClick, onCellContextMenu, onHeaderContextMenu, onCellValueChanged, onSelectionChange, onPageChange, onColumnResize, onRowDrop, onStateChange
#cell-<id> slotprop cellRenderers: { [id]: (params) => node } ("*" for all columns)
#header-<id> slotprop headerRenderers: { [id]: (params) => node }
#filter-<id> slotprop filterRenderers: { [id]: ({ column, value, setValue, api }) => node }
#editor-<id> slotcolumn option editComponent, see custom editors
#statusbar slotrenderStatusBar={({ items, info, api }) => node}
#detail slotrenderDetail={({ row, data, api }) => node}
#group slotrenderGroup={(params) => node}
#empty, #loadingrenderEmpty, renderLoading
#toolbar, #footer, #overlaytoolbar, footer, overlay (node or (api) => node)
template refref with DatagridHandle (api, exportCsv, getDataAsCsv, getState, setState, refresh, focus, selectAll, deselectAll, expandAll, collapseAll, autosizeColumns, undo, redo)
classclassName, style

All state props are optional: without them the grid manages the state itself. A state prop is applied whenever its value changes. If the change callback does not accept a new value (the prop stays the same), the grid returns to the value of the prop.

Custom components ​

Components in column definitions (component, headerComponent, filterComponent, editComponent) are React components:

OptionProps
componentvalue, text, row, data, column, setValue, api, modelValue, onChange + componentProps
headerComponentcolumn, text
filterComponentcolumn, value, onChange
editComponentvalue, onChange, column, item, submitChanges(value?), cancelEdit() (the first input inside the component is focused automatically)
tsx
const columns: ColumnConfig<Task>[] = [
  { field: "status", component: ({ value }) => <StatusBadge status={value} /> },
];

Performance ​

  • Keep columns and options stable (useMemo or module constants). The grid compares them deeply (functions by their source code, so inline functions like rowId: (d) => d.id do not cause updates), but stable references avoid unnecessary work.
  • Renderer props (cellRenderers, renderDetail, …) re-render all visible cells when their identity changes, so memoize them with useMemo / useCallback if they do not depend on changing state.
  • Rows and cells are memoized and only re-render when their data, layout or state changes.

Released under the ISC License.