Skip to content
Niko Table

Persistence

Turn grid edits into create, update, and delete change sets with useGridChanges.

Niko Grid does not track saves on its own. Add useGridChanges to watch edits and get an exact list of creates, updates, and deletes to send to your server. It is plain React and installs separately.

grid-save.tsx
All changes saved
NameEmailRole
Preview with Controlled State
grid-save-state.tsx
All changes saved
NameEmailRole
Current Grid State
Live view of the grid engine state for demonstration
Focused Cell:None
Editing Cell:None
Selection Anchor:None
Total Rows:2
Is Dirty:No
Dirty Row Ids:0
Failed Row Ids:0
Can Undo / Redo:No / No
Last Commit:None
pnpm dlx shadcn@latest add @niko-table/data-table-grid @niko-table/data-table-grid-changes

Pass the same initialRows you gave the grid. Those ids count as “existing”. Any other id is a create. For a blank import, pass [].

const grid = useDataGrid<Row>({ columnIds, createEmptyRow, initialRows })
const changes = useGridChanges(grid, { initialRows })
changes.isDirty
changes.getChangeSet() // { created, updated, deleted }
changes.reconcile(result)
Member Purpose
dirtyRowIds / isDirty Reactive dirty set
getChangeSet() Precise delta for save
reconcile({ succeededIds, failedIds }) Settle after save
failedRowIds Rows that failed last reconcile
reset(rows?) Re-baseline

Dirty checks cost O(1) per keystroke. getChangeSet() does the exact pass. You can pass isEqual so a cell changed back to its original value drops out of updated.

async function onSave() {
const { created, updated, deleted } = changes.getChangeSet()
const result = await api.bulkSave({ created, updated, deleted })
changes.reconcile({
succeededIds: result.succeededIds,
failedIds: result.failedIds,
})
}

Debounce the network call off dirtyRowIds. Its identity changes only when dirtiness changes:

React.useEffect(() => {
if (!changes.isDirty) return
const t = setTimeout(async () => {
const delta = changes.getChangeSet()
const result = await api.bulkSave(delta)
changes.reconcile(result)
}, 800)
return () => clearTimeout(t)
}, [changes.dirtyRowIds])

Driven by grid.lastCommit: { kind, ids?, seq }. seq goes up only on real commits. You can read it directly for badges or analytics.