Migrating to Material React Table V2 from V1
New Feature Highlights
The new optional, but recommended
useMaterialReactTablehook that allows you to create thetableinstance in your own scope. All examples in the docs will use this pattern going forward.Greatly improved Editing and Creating features.
New Row Pinning Features
New Column Filtering
'popover'display mode to give a more "excel-like" filtering experience.New Date Filter variants.
Breaking Changes
@mui/x-date-pickersis now a required peer dependency. Install it with
MaterialReactTableis now a named export instead of a default export. Use curly braces to import it.
- import MaterialReactTable from 'material-react-table'+ import { MaterialReactTable, useMaterialReactTable } from 'material-react-table'
There is no longer a
tableInstanceRefprop. It has been replaced by theuseMaterialReactTablehook, which is way easier to use. It will also be recommended that all table options should be passed to the newuseMaterialReactTablehook instead as props to the<MaterialReactTable />component. See below for more details.
Renamed Props/Options
editingMode->editDisplayModeenablePinning->enableColumnPinningandenableRowPinningvirtualizerInstanceRefsplit intocolumnVirtualizerRefandrowVirtualizerRefvirtualizerPropssplit intocolumnVirtualizerOptionsandrowVirtualizerOptionscolumnVirtualizerProps->columnVirtualizerOptionsrowVirtualizerProps->rowVirtualizerOptionsmuiTableBodyCellCopyButtonProps->muiCopyButtonPropsmuiTableBodyCellEditTextFieldProps->muiEditTextFieldPropsmuiTableBodyCellSkeletonProps->muiSkeletonPropsmuiTableBodyRowDragHandleProps->muiRowDragHandlePropsmuiTableDetailPanelProps->muiDetailPanelPropsmuiTableHeadCellColumnActionsButtonProps->muiColumnActionsButtonPropsmuiTableHeadCellDragHandleProps->muiColumnDragHandlePropsmuiTableHeadCellFilterCheckboxProps->muiFilterCheckboxPropsmuiTableHeadCellFilterTextFieldProps->muiFilterTextFieldPropsmuiTableHeadCellFilterSliderProps->muiFilterSliderProps
useMaterialReactTable
Passing all table options as props to <MaterialReactTable /> will still work, but there is a new and better way to define table options with the useMaterialReactTable hook.
For example, here is a classic example for how to use Material React Table in V1 (still works in V2):
import { MaterialReactTable } from 'material-react-table';export const MyTableComponent = () => {// const tableInstanceRef = useRef(); //deprecatedreturn (//Defining table options as props to <MaterialReactTable /> still works (as long as you don't also pass in a table prop)<MaterialReactTablecolumns={columns}data={data}enableRowSelection //table options as props// tableInstanceRef={tableInstanceRef} //deprecated/>);};
But now, you can define all table options in the useMaterialReactTable.
import {MaterialReactTable,useMaterialReactTable,} from 'material-react-table';export const MyTableComponent = () => {const table = useMaterialReactTable({columns,data,enableRowSelection: true, //table options as options to this hook});return (<MaterialReactTabletable={table} //only pass in table instead of all table options/>);};
Why is useMaterialReactTable Better?
There are a few reasons why having full access to the table instance is better than having MRT create it under the hood for you.
No more need for a confusing
tableInstanceRefprop that doesn't properly cause re-renders when the table instance changes. Now any component that consumes thetableinstance will properly re-render when the table instance changes.Allows for you to not need to use all of Material React Table's components. For example, if you only want to use the
Tablecomponent with no TableContainer or Toolbars, you can simply import a different component from Material React Table.
import { MRT_Table, useMaterialReactTable } from 'material-react-table';export const MyTableComponent = () => {const table = useMaterialReactTable({columns,data,enableRowSelection: true,});const selectedRows = table.getSelectedRowModel().rows;console.log(selectedRows);return (//this internal sub-component does not include the Paper, TableContainer, or Toolbars (lighter weight)<MRT_Table table={table} />);};