Skip to content

React

next-grid-react wraps next-grid-js/core as a React <NextGrid /> component with portal-mounted cell renderers and editors.

Terminal window
npm install next-grid-react next-grid-js react react-dom

Peers: next-grid-js 0.1, react and react-dom 18 or 19.

import { useMemo, useRef } from 'react';
import type { Grid } from 'next-grid-js/core';
import { NextGrid } from 'next-grid-react';
import 'next-grid-js/styles.css';
interface Order {
id: string;
customer: string;
total: number;
}
export function Orders({ orders }: { orders: Order[] }) {
const grid = useRef<Grid<Order>>(null);
const columnDefs = useMemo(
() => [
{ colId: 'customer', key: 'customer' as const, isSortable: true },
{ colId: 'total', key: 'total' as const, cellDataType: 'number' as const },
],
[],
);
return (
<NextGrid<Order>
ref={grid}
style={{ height: 400 }}
columnDefs={columnDefs}
rowData={orders}
getRowId={(order) => order.id}
onSortChanged={(event) => console.log(event.sort)}
/>
);
}

NextGrid accepts all GridOptions<T> except container, plus className, style, and id. Because it builds on next-grid-js/core, pass optional modules (ExportModule, ClipboardModule) in modules.

  • Keep columnDefs stable with useMemo: Columns whose definition object reference is unchanged keep their width, pin, order, and filter/sort state.
  • Mutable props: columnDefs, columnGroups, defaultColDef, rowData, rowSource, datasource, viewportDatasource, hierarchy options, pivot, pivotColumnLimit, cellSpans, pinnedRows, pinnedTotals, theme, and on* callbacks update the live grid automatically.
  • Construction-only props: Changing props without setters (such as density, localeText, rowHeight, modules) logs a development warning; pass a new key to remount intentionally.
  • ref: Exposes the underlying Grid<T> instance (null before mount and after unmount).
import {
reactCellEditor,
reactCellRenderer,
type CellEditorProps,
type CellRendererProps,
} from 'next-grid-react';
function Total({ value }: CellRendererProps<Order>) {
return <b>{String(value)}</b>;
}
function TotalEditor({ value, setValue, commit }: CellEditorProps<Order>) {
return (
<input
defaultValue={String(value)}
onChange={(e) => setValue(Number(e.target.value))}
onBlur={commit}
/>
);
}
const columnDefs = [
{
colId: 'total',
key: 'total' as const,
isEditable: true,
cellRenderer: reactCellRenderer(Total),
cellEditor: reactCellEditor(TotalEditor),
},
];

Components mount through React portals inside your component tree, preserving theme, router, and store context. reactCellRenderer, reactPinnedCellRenderer, and reactCellEditor return stable factory functions per component.

  • React StrictMode: Double-mounting in development cleanly destroys the first grid instance before creating the second.
  • SSR / Next.js: Nothing is instantiated on the server; <NextGrid /> renders an empty host element on SSR and mounts on the client without hydration warnings (mark parent client components with 'use client' in Next.js App Router).