Undo and redo options
Look up the options and the result of useDataGridHistory and useDataGridState, the hooks of the data-grid-history add-on.
The data-grid-history add-on has two hooks. useDataGridHistory adds a history to a grid whose data you own. useDataGridState owns the data array and the history together. Both options objects are optional. Undo and redo explains the behavior.
This sample sets all four options of useDataGridHistory:
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" },
]);
export function HistoryGrid({ rows }: { rows: Row[] }) {
const [data, setData] = useState<readonly Row[]>(rows);
const history = useDataGridHistory<Row>({
capacity: 50,
datasetKey: "2026",
recordSources: ["edit", "paste", "fill"],
onSkip: ({ direction, rowIds }) => {
console.info(`Cannot ${direction}: ${rowIds.length} rows are missing`);
},
});
return (
<DataGrid
data={data}
columns={columns}
onDataChange={setData}
plugins={[history.plugin]}
/>
);
}The stack keeps the last 50 entries. Only edits, pastes, and fills go on it. If an undo or a redo finds a needed row missing, onSkip logs it.
UseDataGridHistoryOptions
Prop
Type
To record streams too, pass recordSources: (defaults) => [...defaults, "stream"].
capacitydefaults to100. It counts entries, not operations. A change ofcapacitybetween renders keeps the entries and trims the oldest.recordSourcesdefaults toedit,paste,fill,delete,cut,row-operation,import,replace, andapp. The default leaves outstream. Pass a list to replace the default, or a function that receives the default list and returns the list to use.- The history never records
history,rollback,merge, orid-swap, whatever the list says. - A change of
datasetKeybetween renders clears both stacks. The first render clears nothing.
UseDataGridHistoryResult
Prop
Type
UseDataGridStateOptions
useDataGridState(defaultRows, options) takes the starting rows as its first argument.
Prop
Type
getRowId defaults to the id field of a row. Every option of useDataGridHistory works the same way here, including recordSources and onSkip. The plugins option takes the other plugins of the grid, such as the fill plugin: useDataGridState(rows, { plugins: [fill.plugin] }). The returned plugins holds them, then the history plugin.
UseDataGridStateResult
Spread the result onto the grid: <DataGrid {...grid} columns={columns} />. The history member is not a grid prop. It holds the controls for your own buttons.
Prop
Type
Related
- Undo and redo: how the history works and how to wire it.
- Undo for code changes:
record, the change sources, anddatasetKeyin use. - Fill handle options: the options of the add-on whose fills the history records.
Fill handle options
Look up the options of useDataGridFill, the ProcessFillContext that processFill receives, and the SeriesDescriptor that a custom detectSeries returns.
Lookup options
Look up the column options of the lookup cell type, the request that fetchOptions receives, the editor labels, and the stored value types.