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#

MemberTypeDescription
elementHTMLDivElement | nullThe grid's scrollable viewport element.
tableHTMLTableElement | nullThe grid's <table> element.

Reading state#

MemberReturnsDescription
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()ColumnSizingStateColumn widths, keyed by column id.
getColumnOrder()ColumnOrderStateColumn ids in their current render order.
getColumnSort()ColumnSortStateThe active sort, in priority order.
getGroupBy()GroupByStateThe active group-by stack, outer to inner. See row grouping.
getGroupExpansion()GroupExpansionStateGroup ids currently collapsed.
getPagination()PaginationStateThe active page and page size. See pagination.
getPageCount()numberHow many pages the current page size splits the rows into — 1 whenever paginated is off.
getAggregates()AggregateResultsThe grand-total aggregate results. See aggregate functions.
getRowSelection()SelectionStateSelected row ids.
getColumnSelection()SelectionStateSelected column ids.
getCellSelection()CellSelectionStateThe 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>:

FieldTypeDescription
rowIdstringThis row's id, as getRowId (or the position fallback) resolved it.
rowRowThe row itself.
rowIndexnumberPosition among the rows as rendered — filtered and sorted, and paginated once paginated is on.
datasetIndexnumberThis row's absolute position in the whole filtered/sorted/grouped dataset, unaffected by the page.

ResolvedColumn<Row>:

FieldTypeDescription
columnColumnDefinition<Row>The column definition this was resolved from.
idstringThe column's id.
widthnumberThis column's current width, in px.
sizedbooleanWhether width came from a user resize rather than the column definition or an auto-fit.
labelNode | stringWhat the header shows: the column's own headerTemplate, or a label read off the field path.
resizablebooleanWhether this column can be resized, after the grid-level default.
reorderablebooleanWhether this column can be dragged, after the grid-level default.
groupByDraggablebooleanWhether 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#

MemberDescription
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:

HookReads
usePaginationStategetPagination, getPageCount — see pagination.
useGroupByStategetGroupBy, getGroupExpansion — see row grouping.
useColumnSortStategetColumnSort — see column sorting.
useSelectionStategetRowSelection, getColumnSelection, getCellSelection — see row selection.
useAggregateStategetAggregates — see aggregate functions.
useColumnSizingStategetColumnSizing.
useColumnOrderStategetColumnOrder.

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 off getDisplayRows, in depth.
Edit this page on GitHub