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-9474
Frozen Concentrated Orange Juice
Joseph Francis
67,727
D-8871
Soybean Meal
Clarence Gordon
97,115
D-31
Soybeans
George Santiago
77,512
D-8064
Rapeseed
Lucy Norton
14,037
D-6666
Sugar No.14
Cole Pierce
53,606
D-5342
Sugar No.14
Cynthia Ferguson
92,948
D-663
Oats
George Figueroa
63,086
D-8641
Soybeans
Katherine Mack
62,986

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-7361
Wheat
Mark Colon
45,778
D-5433
Cotton No.2
Theresa Tucker
59,235
D-8590
Soybeans
Katherine Ball
43,058
D-9331
Soybeans
Callie Matthews
55,249
D-4073
Wheat
Sophie Pearson
24,454
D-4334
Milk
Lenora Hammond
51,934
D-1730
Wheat
Shane Armstrong
18,145
D-6998
Milk
Ruby Graham
22,814
D-3479
Frozen Concentrated Orange Juice
Mayme Barker
13,547

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