MRT logoMaterial React Table

Selection Feature Guide

Material React Table has a built-in row-selection feature and makes it easy to manage the selection state yourself. This guide will walk you through how to enable row selection and how to customize the selection behavior.

Relevant Props

#
Prop Name
Type
Default Value
More Info Links
1
boolean

No Description Provided... Yet...

2
boolean

No Description Provided... Yet...

3
boolean
true

No Description Provided... Yet...

4
boolean
true

No Description Provided... Yet...

5
CheckboxProps | ({ table }) => CheckboxProps
Material UI Checkbox Props

No Description Provided... Yet...

6
CheckboxProps | ({ row, table }) => CheckboxProps
Material UI Checkbox Props

No Description Provided... Yet...

7

No Description Provided... Yet...

8
'all' | 'page'
page

No Description Provided... Yet...

Enable Selection

Selection checkboxes can be enabled with the enableRowSelection prop.

Manage Selection State

The row selection state is managed internally by default, but more than likely, you will want to have access to that state yourself. If you are not already fully managing the table state, like in this guide, then here is how you can simply get access to the row selection state, specifically.

const [rowSelection, setRowSelection] = useState({});
return (
<MaterialReactTable
data={data}
columns={columns}
enableRowSelection
onRowSelectionChange={setRowSelection}
state={{ rowSelection }}
/>
);

Useful Row IDs

By default, the row.id for each row in the table is simply the index of the row in the table. You can override this and tell Material React Table to use a more useful Row ID with the getRowId prop. For example, you may want some like this:

<MaterialReactTable
data={data}
columns={columns}
enableRowSelection
getRowId={(originalRow) => originalRow.userId}
/>

Now as rows get selected, the rowSelection state will look like this:

{
"3f25309c-8fa1-470f-811e-cdb082ab9017": true,
"be731030-df83-419c-b3d6-9ef04e7f4a9f": true,
...
}

This can be very useful when you are trying to read your selection state and do something with your data as the row selection changes.


First Name
Last Name
Age
Address
City
State
DylanMurray22261 Erdman FordEast DaphneKentucky
RaquelKohler18769 Dominic GroveColumbusOhio

Rows per page

1-2 of 2

Source Code

1import React, { useEffect, useMemo, useState } from 'react';
2import MaterialReactTable from 'material-react-table';
3
4const data = [
5 {
6 userId: '3f25309c-8fa1-470f-811e-cdb082ab9017', //we'll use this as a unique row id
7 firstName: 'Dylan',
8 lastName: 'Murray',
9 age: 22,
10 address: '261 Erdman Ford',
11 city: 'East Daphne',
12 state: 'Kentucky',
13 }, //data definitions...
24];
25
26const Example = () => {
27 const columns = useMemo(
28 //column definitions...
56 );
57
58 //optionally, you can manage the row selection state yourself
59 const [rowSelection, setRowSelection] = useState < RowSelectionState > {};
60
61 useEffect(() => {
62 //do something when the row selection changes...
63 console.info({ rowSelection });
64 }, [rowSelection]);
65
66 return (
67 <MaterialReactTable
68 columns={columns}
69 data={data}
70 enableRowSelection
71 getRowId={(row) => row.userId} //give each row a more useful id
72 onRowSelectionChange={setRowSelection} //connect internal row selection state to your own
73 state={{ rowSelection }} //pass our managed row selection state to the table to use
74 />
75 );
76};
77
78export default Example;
79

Disable Select All

By default, if you enable selection for each row, there will also be a select all checkbox in the header of the checkbox column. It can be hidden with the enableSelectAll prop.

<MaterialReactTable
data={data}
columns={columns}
enableSelectAll={false}
enableRowSelection
/>

Customize Select Checkboxes

The selection checkboxes can be customized with the muiSelectCheckboxProps prop. Any prop that can be passed to a Mui Checkbox component can be specified here. For example, you may want to use a different color for the checkbox, or use some logic to disable certain rows from being selected.

<MaterialReactTable
data={data}
columns={columns}
enableRowSelection
muiSelectCheckboxProps={{
color: 'secondary',
}}
/>

Select
First Name
Last Name
Age
Address
City
State
DylanMurray22261 Erdman FordEast DaphneKentucky
RaquelKohler18769 Dominic GroveColumbusOhio
ErvinReinger20566 Brakus InletSouth LindaWest Virginia
BrittanyMcCullough21722 Emie StreamLincolnNebraska
BransonFrami3232188 Larkin TurnpikeCharlestonSouth Carolina

Rows per page

1-5 of 5

Source Code

1import React, { FC, useMemo } from 'react';
2import MaterialReactTable, { MRT_ColumnDef } from 'material-react-table';
3
4const Example: FC = () => {
5 const columns = useMemo(
6 () =>
7 [
8 //column definitions...
34 ] as MRT_ColumnDef<typeof data[0]>[],
35 [],
36 );
37
38 const data = useMemo(
39 () => [
40 //data definitions...
82 ],
83 [],
84 );
85 return (
86 <MaterialReactTable
87 columns={columns}
88 data={data}
89 enableSelectAll={false}
90 enableRowSelection
91 muiSelectCheckboxProps={({ row }) => ({
92 color: 'secondary',
93 disabled: row.getValue<number>('age') < 21,
94 })}
95 />
96 );
97};
98
99export default Example;
100