Imperative handle
The DataGridApi reached through DataGridComponent's ref — reading live grid state and triggering focus, scroll, and selection actions.
Last updated August 24, 2026
DataGridComponent accepts a ref typed Ref<DataGridApi<Row>>, for the state and actions that aren't otherwise reachable from outside: the grid's DOM nodes, its live column sizing/order/sort and selection, and the handful of actions every grid ref carries.
import { useRef } from "react";
import { DataGridComponent, type DataGridApi } from "@gridkitjs/react";
const gridRef = useRef<DataGridApi<Row>>(null);
<DataGridComponent ref={gridRef} columns={columns} dataSource={rows} />;
gridRef.current?.scrollToRow("42");
gridRef.current?.getColumnSizing();Sizing, order, sort, and selection stay uncontrolled exactly as they are without a ref — set their starting values with defaultColumnSizing and friends. DataGridApi is for reading current state and triggering the actions below, not a second way to drive that same state. subscribe doesn't change that either: it's a read/notify channel for reacting to state that already lives here, not a write path.
Elements#
| Member | Type | Description |
|---|---|---|
element | HTMLDivElement | null | The grid's scrollable viewport element. |
table | HTMLTableElement | null | The grid's <table> element. |
Reading state#
| Member | Returns | Description |
|---|---|---|
getRows() | readonly ResolvedRow<Row>[] | Rows as currently filtered and sorted — what's rendered, ungrouped. |
getDisplayRows() | readonly DisplayRow<Row>[] | getRows() regrouped into the active groupBy — group headers interleaved with data rows, in render order. Identical to getRows() when groupBy is empty. Each group header's aggregates field carries that group's own computed results; under groupAggregateDisplay: "row", a group's own summary row (kind: "group-summary") is interleaved right after it, carrying the same aggregates. |
getColumns() | readonly ResolvedColumn<Row>[] | Columns as currently sized and ordered — what's rendered. |
getColumnSizing() | ColumnSizingState | Column widths, keyed by column id. |
getColumnOrder() | ColumnOrderState | Column ids in their current render order. |
getColumnSort() | ColumnSortState | The active sort, in priority order. |
getGroupBy() | GroupByState | The active group-by stack, outer to inner. See row grouping. |
getGroupExpansion() | GroupExpansionState | Group ids currently collapsed. |
getPagination() | PaginationState | The active page and page size. See pagination. |
getPageCount() | number | How many pages the current page size splits the rows into — 1 whenever paginated is off. |
getAggregates() | AggregateResults | The grand-total aggregate results. See aggregate functions. |
getRowSelection() | SelectionState | Selected row ids. |
getColumnSelection() | SelectionState | Selected column ids. |
getCellSelection() | CellSelectionState | The selected cell, if any. |
getFocusedCell() | { rowIndex: number; columnIndex: number } | The cell currently holding the grid's single tab stop. |
Resolved row and column#
getRows() and getColumns() return ResolvedRow<Row> and ResolvedColumn<Row> — the same shapes the selection events carry, resolved once here so no other doc page repeats them.
ResolvedRow<Row>:
| Field | Type | Description |
|---|---|---|
rowId | string | This row's id, as getRowId (or the position fallback) resolved it. |
row | Row | The row itself. |
rowIndex | number | Position among the rows as rendered — filtered and sorted, and paginated once paginated is on. |
datasetIndex | number | This row's absolute position in the whole filtered/sorted/grouped dataset, unaffected by the page. |
ResolvedColumn<Row>:
| Field | Type | Description |
|---|---|---|
column | ColumnDefinition<Row> | The column definition this was resolved from. |
id | string | The column's id. |
width | number | This column's current width, in px. |
sized | boolean | Whether width came from a user resize rather than the column definition or an auto-fit. |
label | Node | string | What the header shows: the column's own headerTemplate, or a label read off the field path. |
resizable | boolean | Whether this column can be resized, after the grid-level default. |
reorderable | boolean | Whether this column can be dragged, after the grid-level default. |
groupByDraggable | boolean | Whether this column's header may be dragged into the group-by bar, after the grid-level default. |
alignment | "left" | "center" | "right" | How this column's cells align, after falling back to its type. |
Actions#
| Member | Description |
|---|---|
focusCell(rowIndex, columnIndex) | Moves the grid's tab stop, and the browser's focus, to a cell. |
clearSelection() | Clears row, column, and cell selection together — the same as pressing Escape. |
selectAllRows() | Selects every row — the same as Ctrl+A. |
expandAllGroups() | Expands every group at once — there's no dedicated UI control for it. |
collapseAllGroups() | Collapses every group currently shown at once. |
goToPage(pageIndex) | Moves to the given page, clamped into range. |
nextPage() | Moves to the next page. A no-op on the last page. |
previousPage() | Moves to the previous page. A no-op on the first page. |
setPageSize(pageSize) | Changes the page size, resetting to the first page. |
scrollToRow(rowId, options?) | Scrolls the row with the given id into view, if it is currently shown. |
scrollToColumn(columnId, options?) | Scrolls the column with the given id into view. |
options on scrollToRow/scrollToColumn is a standard
ScrollIntoViewOptions,
passed straight through.
Subscribing to state changes#
subscribe(listener: () => void): () => void;Registers listener to be called after any render in which the grid's internal state may have changed — any getter above may return a different value once it fires, not just one. Returns an unsubscribe function.
Most consumers should reach for one of the use*State hooks built on it instead of calling subscribe directly — each wraps it in useSyncExternalStore so a component elsewhere in the tree stays reactive with no on*Change prop to forward and no polling:
| Hook | Reads |
|---|---|
usePaginationState | getPagination, getPageCount — see pagination. |
useGroupByState | getGroupBy, getGroupExpansion — see row grouping. |
useColumnSortState | getColumnSort — see column sorting. |
useSelectionState | getRowSelection, getColumnSelection, getCellSelection — see row selection. |
useAggregateState | getAggregates — see aggregate functions. |
useColumnSizingState | getColumnSizing. |
useColumnOrderState | getColumnOrder. |
Each hook falls back to an empty/zero value before the grid mounts, the same way its matching getter would if called too early.
See also#
- DataGridComponent for the rest of the component's props.
- Events for callbacks that report the same interactions as they happen, rather than being queried on demand.
- Row grouping for
getGroupBy/getGroupExpansion/getDisplayRows/expandAllGroups/collapseAllGroups, in depth. - Pagination for
getPagination/getPageCount/goToPage/nextPage/previousPage/setPageSize, in depth. - Aggregate functions for
getAggregates, and reading a group's own results offgetDisplayRows, in depth.