Skip to content
+

Data Grid - Custom slots and subcomponents

Learn how to override parts of the grid.

Interacting with the data grid

The grid exposes two hooks to help you access the data grid data while overriding component slots.

They can be used as below:

  • useGridApiContext: returns the apiRef object (more details on the API object page).
  • useGridSelector: returns the result of a selector on the current state (more details on the State page).
function CustomPagination() {
  const apiRef = useGridApiContext();
  const paginationModel = useGridSelector(apiRef, gridPaginationModelSelector);
  const pageCount = useGridSelector(apiRef, gridPageCountSelector);

  return (
    <Pagination
      count={pageCount}
      page={paginationModel.page + 1}
      onChange={(event, value) => apiRef.current.setPage(value - 1)}
    />
  );
}

Component slots

Column menu

As mentioned above, the column menu is a component slot that can be recomposed easily and customized on each column as in the demo below.

Toolbar

To enable the toolbar you need to add the toolbar: GridToolbar to the Data Grid slots prop. This demo showcases how this can be achieved.

Press Enter to start editing

You can also compose your own toolbar. Each button in the toolbar is wrapped with a tooltip component. In order to override some of the props corresponding to the toolbar buttons, you can use the slotProps prop.

The following demo shows how to override the tooltip title of the density selector and the variant of the export button.

function CustomToolbar() {
  return (
    <GridToolbarContainer>
      <GridToolbarColumnsButton />
      <GridToolbarFilterButton />
      <GridToolbarDensitySelector
        slotProps={{ tooltip: { title: 'Change density' } }}
      />
      <Box sx={{ flexGrow: 1 }} />
      <GridToolbarExport
        slotProps={{
          tooltip: { title: 'Export data' },
          button: { variant: 'outlined' },
        }}
      />
    </GridToolbarContainer>
  );
}
Press Enter to start editing

The grid exposes props to hide specific elements of the UI:

  • hideFooter: If true, the footer component is hidden.
  • hideFooterRowCount: If true, the row count in the footer is hidden.
  • hideFooterSelectedRowCount: If true, the selected row count in the footer is hidden.
  • hideFooterPagination: If true, the pagination component in the footer is hidden.

Pagination

The default pagination component is exported as GridPagination. This component is an extension of the TablePagination component, and it renders the page size control, the number of rows in the page and also the buttons to go to the previous and next page. You can replace the pagination component completely or reuse the default one.

The next demo reuses GridPagination but replaces the previous and next page buttons with Pagination, which renders a dedicated button for each page.

Press Enter to start editing

Row

The slotProps.row prop can be used to pass additional props to the row component. One common use case might be to listen for events not exposed by default. The demo below shows a context menu when a row is right-clicked.

Cell

The following demo uses the slotProps.cell prop to listen for specific events emitted by the cells. Try it by hovering a cell with the mouse and it should display the number of characters each cell has.

Icons

As any component slot, every icon can be customized. However, it is not yet possible to use the slotProps with icons.

Press Enter to start editing

Overlays

See the Overlays documentation on how to customize the loadingOverlay, noRowsOverlay, and noResultsOverlay.

Custom slot props with TypeScript

If the custom component requires additional props to work properly, TypeScript may throw type errors. To solve these type errors, use module augmentation to enhance the props interface.

The naming of overridable interfaces uses a pattern like this:

`${slotNameInPascalCase}PropsOverrides`;

For example, for columnMenu slot, the interface name would be ColumnMenuPropsOverrides.

This file lists all the interfaces for each slot that could be used for augmentation.

// augment the props for the toolbar slot
declare module '@mui/x-data-grid' {
  interface ToolbarPropsOverrides {
    someCustomString: string;
    someCustomNumber: number;
  }
}

<DataGrid
  slots={{
    // custom component passed to the toolbar slot
    toolbar: CustomGridToolbar,
  }}
  slotProps={{
    toolbar: {
      // props used by CustomGridToolbar
      someCustomString: 'Hello',
      someCustomNumber: 42,
    },
  }}
/>;

This demo below shows how to use the slotProps prop and module augmentation to pass a new prop status to the footer slot.