Undo for code changes
Make changes that your code applies undoable with record or a change source, pick which sources the history keeps, and reset it when you swap the data.
History records the changes that go through the grid's write path. A change that your code makes by replacing the data prop does not go through it, so the history never sees it. To make such a change undoable, apply it with record, or choose which sources the history keeps. Undo and redo shows the basic setup.
Programmatic changes: record
record(change, label?) applies a change to the grid as one write and registers it as one undo entry. You build the change from operations (ops) that address rows by row id. record sets the source to "app". It returns true when it wrote the change. It returns false when it wrote nothing: before mount, on a readOnly grid, or when each operation inserts a row that data already holds.
record is idempotent for inserts. It skips an insert of a row that data already holds and warns in development. The call writes the change itself, so do not apply it to your own array first.
This button adds a row that Ctrl+Z removes:
import { useState } from "react";
import { DataGrid, defineColumns } from "@/components/data-grid/data-grid";
import { useDataGridHistory } from "@/components/data-grid-history/data-grid-history";
type Row = { id: string; name: string };
const columns = defineColumns<Row>()([
{ id: "name", header: "Name", accessorKey: "name" },
]);
function MyGrid() {
const [data, setData] = useState<readonly Row[]>([]);
const history = useDataGridHistory<Row>();
const addRow = () => {
const row = { id: crypto.randomUUID(), name: "New row" };
history.record(
{
ops: [{ type: "insert", rowId: row.id, row, index: data.length }],
},
"Add row",
);
};
return (
<>
<button onClick={addRow}>Add row</button>
<DataGrid
data={data}
columns={columns}
onDataChange={setData}
plugins={[history.plugin]}
/>
</>
);
}Do not also set your own state. The grid emits the new array through onDataChange, and your handler stores it. The label travels with the entry. No API reads the entries back.
Which changes are recorded
Every change carries a source. By default the history records these sources:
| Source | Origin |
|---|---|
edit | A cell edit |
paste | A paste |
fill | A fill |
delete | Clearing the cells of a selection |
cut | Clearing the cells that a cut copied |
row-operation | An insert, duplicate, delete, or move of rows |
import | The import dialog, or a change that your code tags as an import |
replace | A find and replace |
app | record |
The history never records history (an undo or a redo), rollback, merge, or id-swap, whatever the list says. It skips stream by default, so a live feed of updateCells batches cannot fill the stack. If you pass no source, updateCells tags its batch "stream". To make a batch undoable, pass source: "edit". Render the component under the grid's provider, for example in a toolbar:
import { useDataGridApi } from "@/components/data-grid/data-grid";
function ClearNameButton({ rowId }: { rowId: string }) {
const { actions } = useDataGridApi();
const clear = () =>
actions.updateCells(
[{ rowId, columnId: "name", value: "" }],
{ source: "edit" },
);
return <button onClick={clear}>Clear name</button>;
}To change the list, pass recordSources to useDataGridHistory. An array replaces the list, for example ["edit", "paste", "stream"]. A function receives the defaults, so this hook keeps them and adds one source:
import { useDataGridHistory } from "@/components/data-grid-history/data-grid-history";
type Row = { id: string; name: string };
export function useHistoryWithStream() {
return useDataGridHistory<Row>({
recordSources: (defaults) => [...defaults, "stream"],
});
}Data swaps
After you swap the data, the entries still describe the old rows, and an undo can insert them back. Pass an identity of the data as datasetKey. When the key changes between renders, the hook clears both stacks. The first render clears nothing. This component swaps the data:
import { useState } from "react";
import { DataGrid, defineColumns } from "@/components/data-grid/data-grid";
import { useDataGridHistory } from "@/components/data-grid-history/data-grid-history";
type Row = { id: string; name: string };
const datasets: Record<string, Row[]> = {
"2025": [{ id: "a", name: "Ada" }],
"2026": [{ id: "b", name: "Grace" }],
};
const columns = defineColumns<Row>()([
{ id: "name", header: "Name", accessorKey: "name" },
]);
function MyGrid() {
const [datasetId, setDatasetId] = useState("2025");
const [data, setData] = useState<readonly Row[]>(datasets["2025"]!);
const history = useDataGridHistory<Row>({ datasetKey: datasetId });
const openDataset = (id: string) => {
setData(datasets[id]!);
setDatasetId(id);
};
return (
<>
<button onClick={() => openDataset("2026")}>Open 2026</button>
<DataGrid
data={data}
columns={columns}
onDataChange={setData}
plugins={[history.plugin]}
/>
</>
);
}An id of the data and a version counter both work. The key only has to change when the data changes. For a reset without a key, such as a reset button, call clear().