Skip to content

Data Grid - API object

Interact with the Data Grid using its API.

The API object is an interface containing the state and all the methods available to programmatically interact with the Data Grid.

You can find the list of all the API methods on the GridApi page.

How to use the API object

The API object is accessible through the apiRef variable. To access this variable, use useGridApiContext (inside the Data Grid) or useGridApiRef (outside the Data Grid).

Inside the Data Grid

To access the API object inside component slots or inside renders (for instance, renderCell or renderHeader), use the useGridApiContext hook:

function CustomFooter() {
  const apiRef = useGridApiContext();

  return <Button onClick={() => apiRef.current.setPage(1)}>Go to page 1</Button>;
}
Desk
Commodity
Trader Name
Trader Email
Quantity
D-5038
Cotton No.2
Georgia Boone
1,966
D-1306
Cotton No.2
Luke Rogers
44,466
D-655
Corn
Clifford Townsend
31,780
D-8163
Oats
Theresa Frank
88,161
D-4023
Sugar No.14
Jack May
1,568
D-6011
Soybean Meal
Randall Stephens
91,263
D-6104
Soybean Oil
Christina Morton
25,182
D-7798
Coffee C
Lizzie Elliott
49,669

1–10 of 100

Outside the Data Grid

When using the API object outside the data grid components, you need to initialize it using the useGridApiRef hook. You can then pass it to the Data Grid's apiRef prop:

function CustomDataGrid(props) {
  const apiRef = useGridApiRef();

  return (
    <div>
      <Button onClick={() => apiRef.current.setPage(1)}>Go to page 1</Button>
      <DataGrid apiRef={apiRef} {...other} />
    </div>
  );
}
Desk
Commodity
Trader Name
Trader Email
Quantity
D-5832
Robusta coffee
Gavin Myers
23,981
D-7640
Sugar No.14
Randall Clarke
76,556
D-2352
Soybeans
Tom Reynolds
3,735
D-2435
Cotton No.2
Ella Morton
97,857
D-3074
Soybeans
Eva Keller
45,698
D-327
Corn
Wesley Page
24,464
D-2906
Oats
Juan Gill
59,039
D-355
Rough Rice
Albert Tyler
34,140
D-2774
Cotton No.2
Jerome Harrington
45,858

1–10 of 100

Common use cases

Retrieve data from the state

You can find a detailed example on the State page.

Listen to grid events

You can find a detailed example on the Events page.

API