gridcn2.0.0
Learning path

Render your first grid

Step 2 of 8

Render an editable grid from three rows and three columns, and learn the keys that edit, commit, and cancel a cell.

Before you start, install the core grid.

This step renders a grid with three columns and three rows. You can edit every cell.

Add the columns

A column maps one field of a row to one grid column.

  • accessorKey names the row field that the column reads.
  • type names the cell type: the kind of value the column holds. It sets how the cell renders and edits.
  • id identifies the column.

defineColumns<Person>() is two calls. The first fixes the row type. The second infers the value type of each column. Add this file:

columns.ts
import { defineColumns } from "@/components/data-grid/data-grid";

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

export const columns = defineColumns<Person>()([
  { id: "name", header: "Name", accessorKey: "name", type: "text" },
  { id: "age", header: "Age", accessorKey: "age", type: "number" },
  { id: "active", header: "Active", accessorKey: "active", type: "checkbox" },
]);

Render the grid

defaultData seeds the grid once. After the first render the grid owns the rows, so edits, pastes, and deletes need no state in your code. Add this file and render <PeopleGrid /> on any page:

people-grid.tsx
"use client";

import { DataGrid } from "@/components/data-grid/data-grid";
import { columns, type Person } from "./columns";

const people: Person[] = [
  { id: "1", name: "Ada Lovelace", age: 28, active: true },
  { id: "2", name: "Grace Hopper", age: 34, active: true },
  { id: "3", name: "Margaret Hamilton", age: 31, active: false },
];

export function PeopleGrid() {
  return (
    <DataGrid
      defaultData={people}
      columns={columns}
      aria-label="People"
      className="h-96"
    />
  );
}

className="h-96" gives the grid a fixed height. aria-label names the grid for screen readers. Without it or aria-labelledby, development mode warns once. Without a bounded height the grid cannot virtualize. It draws at most 200 rows and, when more than 200 rows fit, warns once in development. In a flex column or a CSS grid track, give the grid min-h-0 flex-1 or h-full min-h-0 instead. Without min-h-0, a flex or grid item grows to fit every row.

To keep the rows in your own state, see Keep edits in state.

Row ids

getRowId returns the id of a row. It defaults to the id field of the row, which must be a string or a number. The rows above have that field and need no getRowId. If the id lives in another field, such as uuid, pass getRowId:

<DataGrid
  defaultData={accounts}
  columns={columns}
  getRowId={(account) => account.uuid}
/>

Each id must be stable and unique. Selection, edit sessions, and undo address rows by row id. If you change what getRowId returns, pass a new data array in the same render, or remount a defaultData grid with a new key. Otherwise development mode warns.

Try it

Click a cell, then use these keys:

  • In the Name and Age columns, type a character to replace the value of the cell.
  • In the same columns, press F2, Enter, or Space to edit the value in place. A double-click does the same.
  • In the Active column, a click on the box, Space, or Enter toggles the checkbox. Typing does nothing there.
  • While you edit, press Enter to commit and move down. Press Escape to cancel.
  • Press Delete to empty the selected cells.

A click on a column header selects the whole column. It does not sort. To sort on a header click, see Sort and filter rows.

DataGrid parts

<DataGrid> renders DataGridProvider, DataGridRoot, DataGridHeader, and DataGridBody. To place a toolbar above the grid, compose the parts yourself: the toolbar goes inside DataGridProvider, before DataGridRoot.

Next: Define columns.

On this page