Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

How to read the selected rows in an MUI X Data Grid

ErrorNothing happens when rows are selected and onRowSelected is used

Short answer

The current Data Grid reports selection through onRowSelectionModelChange. It gives you ids, not row objects, and the model is an object with a type and an ids Set. Read the ids from it, then look up the rows in your own data, or call getSelectedRows on the grid API.

  • Applies to MUI X Data Grid current, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Log what the selection callback gives you. In the current grid it is an object, not an array.

    <DataGrid
      rows={rows}
      columns={columns}
      checkboxSelection
      onRowSelectionModelChange={(model) => console.log(model)}
    />
  2. 2Match what you see, then jump to its fix

    1. 1Nothing logs when rows are selectedThe wrong callback is usedFix
    2. 2model.map is not a function, or length is undefinedThe model is treated as an arrayFix
    3. 3You have the ids and want the full recordsOnly ids are available but the row objects are neededFix

What does the error look like?

  • Nothing happens when rows are selected and onRowSelected is used
  • model.map is not a function on the selection value
  • The callback has ids but I need the row objects

Cause 1 of 3

The wrong callback is used

1Check

The documented way to react to selection is the pair rowSelectionModel and onRowSelectionModelChange.

grep -rn "onRowSelected\|onSelectionModelChange" src/

2Fix

Switch to onRowSelectionModelChange, and keep the model in state if you control it.

const [rowSelectionModel, setRowSelectionModel] = React.useState({
  type: 'include',
  ids: new Set(),
});

<DataGrid
  checkboxSelection
  rowSelectionModel={rowSelectionModel}
  onRowSelectionModelChange={setRowSelectionModel}
  rows={rows}
  columns={columns}
/>

3Verify

Selecting a row updates the model in state.

# select a row in the browser

Confirmed by mui.com

Did this fix it?

Cause 2 of 3

The model is treated as an array

1Check

The selection model is an object: type is include or exclude, and ids is a Set. With exclude, every row is selected except those ids, which is what Select all uses.

console.log(model.type, model.ids);

2Fix

Read model.ids as a Set. To always get explicit ids, add disableRowSelectionExcludeModel.

<DataGrid
  checkboxSelection
  disableRowSelectionExcludeModel
  onRowSelectionModelChange={(model) => {
    const ids = Array.from(model.ids);
  }}
  rows={rows}
  columns={columns}
/>

3Verify

ids is a plain array of selected ids, also after Select all.

# click the header checkbox

Confirmed by mui.com

Did this fix it?

Cause 3 of 3

Only ids are available but the row objects are needed

1Check

The callback gives ids. Row data comes from your own rows array or from the grid API.

grep -rn "apiRef" src/

2Fix

Filter your rows by the ids, or ask the grid API for the selected rows, which returns a Map of id to row. The docs call the API a last resort when props are not enough.

const selectedRows = rows.filter(row => model.ids.has(row.id));

// or, with the grid API (pass apiRef={apiRef} to DataGrid)
const apiRef = useGridApiRef();
const selected = apiRef.current.getSelectedRows();

3Verify

selectedRows holds the full objects of the checked rows.

console.log(selectedRows);

Confirmed by mui.com

Did this fix it?

How do I stop it coming back?

  • Keep the selection model in state and treat it as an object with type and ids.
  • Prefer filtering your own data over the grid API when props are enough.
  • Use disableRowSelectionOnClick when cells hold buttons or links.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.