Skip to content
Niko Table

Inline Edit Table

Edit rows in place, and keep memoized rows re-rendering correctly with getRowMemoKey.

Lets users edit fields right in the table row, with draft and validation state kept outside the data. Use it when you want fast inline editing without a dialog. It shows the getRowMemoKey prop, which tells memoized rows when to re-render.

inline-edit.tsx
Name
Category
Price
Stock
Wireless HeadphonesElectronics$79.99120
Running ShoesSports$59.9985
Coffee MakerAppliances$49.99200
Yoga MatSports$29.99310
Desk LampFurniture$39.99150
Bluetooth SpeakerElectronics$89.9965
Water BottleSports$19.99400
Air PurifierAppliances$129.9940
Preview with Controlled State
inline-edit-state.tsx
Name
Category
Price
Stock
Wireless HeadphonesElectronics$79.99120
Running ShoesSports$59.9985
Coffee MakerAppliances$49.99200
Yoga MatSports$29.99310
Desk LampFurniture$39.99150
Current Table State
Live view of the current table state for demonstration purposes
Editing Row:None
Total Items:8
Sorting:None
Page:1 (Size: 5)
Hidden Columns:0
View Full State Object
Sorting:
[]
Pagination:
{
  "pageIndex": 0,
  "pageSize": 5
}
Column Visibility:
{}

The Inline Edit Table lets users edit row fields directly in the table, with no dialog and no page change.

Niko Table wraps each body row in React.memo for speed. Rows only re-render when their props change. That is great for large tables, but it causes a problem here: the edit draft and validation errors live outside the row’s props, so memoized rows don’t see changes by default.

getRowMemoKey fixes this:

  • You return a string for each row that encodes the outside state that row depends on.
  • When the string changes, the row re-renders.
  • When it stays the same, the row stays frozen.

A common approach is to put edit state in the data array. Each row object carries an isEditing: boolean flag. Starting an edit means calling setData to flip the flag. That replaces the whole array, so every memoized row re-renders.

We do it differently: edit state lives outside the data.

  • editingId, draft, and errors are separate useState variables.
  • The data array changes once, on save(), not on every keystroke.
  • Callbacks are passed into column cell closures (or optionally through table.options.meta), not through the data shape.

The result: typing in an input re-renders exactly one memoized row, because getRowMemoKey returns a new string for it. React skips all other rows.

Concern Embedded state Our approach
Edit state location Inside the data array (isEditing on each row) Outside data (editingId + draft state)
Update on keystroke setData replaces the whole array → all rows re-render Only draft changes → one row re-renders via getRowMemoKey
Draft & validation No concept, changes write directly to data Staged draft with error validation before committing
Data mutation On every interaction Once, on explicit save()

Install the DataTable core and add-ons for this example:

pnpm dlx shadcn@latest add @niko-table/data-table @niko-table/data-table-pagination @niko-table/data-table-search-filter @niko-table/data-table-view-menu @niko-table/data-table-column-sort

This example also uses input and badge from Shadcn UI:

pnpm dlx shadcn@latest add input badge

First time using @niko-table? See the Installation Guide to set up the registry.

For other add-ons or manual copy-paste, see the Installation Guide.

We are going to build a table to show products with inline editing. Here’s what our data looks like:

type Product = {
id: string
name: string
category: string
price: number
stock: number
}

Put all edit logic in a reusable hook. The key export is getRowMemoKey:

use-inline-edit.ts
type EditDraft = { name: string; price: string }
type EditErrors = { name?: string; price?: string }
function useInlineEdit(
setData: React.Dispatch<React.SetStateAction<Product[]>>,
) {
const [editingId, setEditingId] = useState<string | null>(null)
const [draft, setDraft] = useState<EditDraft>({ name: "", price: "" })
const [errors, setErrors] = useState<EditErrors>({})
const startEditing = useCallback((product: Product) => {
setEditingId(product.id)
setDraft({ name: product.name, price: String(product.price) })
setErrors({})
}, [])
const cancel = useCallback(() => {
setEditingId(null)
setDraft({ name: "", price: "" })
setErrors({})
}, [])
const setField = useCallback((field: keyof EditDraft, value: string) => {
setDraft((prev) => ({ ...prev, [field]: value }))
setErrors((prev) => ({ ...prev, [field]: undefined }))
}, [])
const save = useCallback(() => {
const next: EditErrors = {}
if (!draft.name.trim()) next.name = "Name is required"
const parsed = parseFloat(draft.price)
if (isNaN(parsed) || parsed <= 0) next.price = "Enter a positive price"
if (Object.keys(next).length > 0) {
setErrors(next)
return
}
setData((prev) =>
prev.map((p) =>
p.id === editingId ? { ...p, name: draft.name.trim(), price: parsed } : p,
),
)
cancel()
}, [draft, editingId, cancel, setData])
/**
* getRowMemoKey — the bridge between external state and React.memo.
*
* Returns "" for every non-editing row (no change → no re-render).
* Returns a unique string for the editing row that encodes all mutable
* state — draft values and validation errors.
*
* Each keystroke changes `draft.name` → new string → React.memo
* re-renders only this row. All 7 other rows are untouched.
*/
const getRowMemoKey = useCallback(
(row: Product): string => {
if (row.id !== editingId) return ""
return `${draft.name}|${draft.price}|${errors.name ?? ""}|${errors.price ?? ""}`
},
[editingId, draft, errors],
)
return { editingId, draft, errors, startEditing, cancel, setField, save, getRowMemoKey }
}

Column cells use the inlineEdit state. They show either the input or the read-only value:

columns.tsx
const columns = useMemo<DataTableColumnDef<Product>[]>(
() => [
{
id: "name",
accessorKey: "name",
header: () => (
<DataTableColumnHeader>
<DataTableColumnTitle>Name</DataTableColumnTitle>
<DataTableColumnSortMenu />
</DataTableColumnHeader>
),
cell: ({ row }) => {
const isEditing = row.original.id === inlineEdit.editingId
if (!isEditing) return <span className="font-medium">{row.original.name}</span>
return (
<Input
value={inlineEdit.draft.name}
onChange={(e) => inlineEdit.setField("name", e.target.value)}
className="h-7 w-48 text-sm"
autoFocus
onKeyDown={(e) => {
if (e.key === "Enter") inlineEdit.save()
if (e.key === "Escape") inlineEdit.cancel()
}}
/>
)
},
},
// ... price, category, stock columns
{
id: SYSTEM_COLUMN_IDS.ACTIONS,
cell: ({ row }) => {
const isEditing = row.original.id === inlineEdit.editingId
if (isEditing) {
return (
<div className="flex gap-1">
<Button size="icon" variant="ghost" onClick={inlineEdit.save}>
<Check className="h-3.5 w-3.5 text-green-600" />
</Button>
<Button size="icon" variant="ghost" onClick={inlineEdit.cancel}>
<X className="h-3.5 w-3.5 text-red-600" />
</Button>
</div>
)
}
return (
<Button
size="icon"
variant="ghost"
onClick={() => inlineEdit.startEditing(row.original)}
>
<Pencil className="h-3.5 w-3.5" />
</Button>
)
},
},
],
// Rebuild columns when edit state changes so cells see fresh closures.
// eslint-disable-next-line react-hooks/exhaustive-deps
[inlineEdit.editingId, inlineEdit.draft, inlineEdit.errors],
)

Pass getRowMemoKey straight to DataTableBody:

products-table.tsx
export function ProductsTable() {
const [data, setData] = useState<Product[]>(initialData)
const inlineEdit = useInlineEdit(setData)
const columns = useMemo(..., [inlineEdit.editingId, inlineEdit.draft, inlineEdit.errors])
return (
<DataTableRoot data={data} columns={columns}>
<DataTableToolbarSection>
<DataTableSearchFilter placeholder="Search products…" />
</DataTableToolbarSection>
<DataTable>
<DataTableHeader />
<DataTableBody getRowMemoKey={inlineEdit.getRowMemoKey}>
<DataTableEmptyBody>...</DataTableEmptyBody>
</DataTableBody>
</DataTable>
<DataTablePagination />
</DataTableRoot>
)
}

getRowMemoKey works with any per-row outside state. To combine several sources, join them with a separator:

composing-multiple-sources.tsx
const [savingIds, setSavingIds] = useState<Set<string>>(new Set())
// useInlineEdit already returns its own getRowMemoKey
const inlineEdit = useInlineEdit(setData)
// Compose: inline edit state + saving state + any other per-row state
const getRowMemoKey = useCallback(
(row: Product): string =>
[
inlineEdit.getRowMemoKey(row), // edit draft + errors
savingIds.has(row.id) ? "saving" : "idle", // in-flight mutation
].join("||"),
[inlineEdit.getRowMemoKey, savingIds],
)
// Works with any body variant — virtualized, DnD, virtualized DnD
return (
<>
{/* Non-virtualized */}
<DataTableBody getRowMemoKey={getRowMemoKey} />
{/* Or virtualized */}
<DataTableVirtualizedBody getRowMemoKey={getRowMemoKey} estimateSize={52} />
{/* Or row-DnD */}
<DataTableDndBody getRowMemoKey={getRowMemoKey} />
</>
)

The example handles these keys in the input’s onKeyDown:

Key Action
Enter Save the edit
Escape Cancel and revert