gridcn2.0.0
Editing cells

Display override

Draw a column's cells with your own React content while the cell type keeps editing, sorting, and copying.

renderCell replaces how a column draws its cells and keeps everything else. The cell type still supplies the editor, the sort order, and the text for copy, paste, and export. Use it for a progress bar, a badge, or an icon on a value that a built-in type already stores and edits. If you need a new editor or a new way to read text, write a cell type instead.

Override the display

Set renderCell on the column and return a React node. The grid adds no label to the cell, and a screen reader reads the text inside your node. Add visually hidden text for a value that the node draws without text. This column draws a score as a bar and keeps the score as hidden text:

import { DataGrid, defineColumns } from "@/components/data-grid/data-grid";

type Player = { id: string; name: string; score: number | null };

const columns = defineColumns<Player>()([
  { id: "name", header: "Name", accessorKey: "name" },
  {
    id: "score",
    header: "Score",
    accessorKey: "score",
    type: "number",
    renderCell: ({ value }) => (
      <div className="h-2 w-full rounded-full bg-muted">
        <span className="sr-only">{value}</span>
        <div
          className="h-full rounded-full bg-primary"
          style={{ width: `${value ?? 0}%` }}
        />
      </div>
    ),
  },
]);

export function Players({ players }: { players: Player[] }) {
  return <DataGrid defaultData={players} columns={columns} />;
}

When the user edits the cell, the type's editor replaces your content. A row that has not loaded yet shows a skeleton, and renderCell does not run for it.

Render the value as React text. Never pass a cell value to dangerouslySetInnerHTML.

Show an image

Pass an image URL from the data through safeHref first. It returns null for every protocol except http, https, and mailto. This column shows a photo:

import { DataGrid, defineColumns, safeHref } from "@/components/data-grid/data-grid";

type Person = { id: string; name: string; photo: string };

const columns = defineColumns<Person>()([
  { id: "name", header: "Name", accessorKey: "name" },
  {
    id: "photo",
    header: "Photo",
    accessorKey: "photo",
    renderCell: ({ value, row }) => {
      const src = safeHref(value);
      if (!src) return null;
      return (
        <img src={src} alt={row.name} className="size-6 rounded-full" />
      );
    },
  },
]);

export function People({ people }: { people: Person[] }) {
  return <DataGrid defaultData={people} columns={columns} />;
}

The cell type stays text, so copy, paste, and export use the URL.

Arguments

renderCell receives one object:

  • value: the cell value, read through accessorKey or accessorFn.
  • row: the full data row.
  • rowId: the row id.
  • columnId: the column id.
  • viewRowIndex: the position in the sorted and filtered view. The position moves with the view, so identify a row by rowId.
  • column: the column definition.
  • options: the column options.
  • isActive: true while the cell is the active cell.

When a cell re-renders

The grid skips a cell whose props did not change. A renderCell that reads other state, such as a role or a theme, draws again only after you build a new columns array. A component that renderCell returns re-renders on its own state, so read changing state inside it.

On this page