How to read the selected rows in an MUI X Data Grid
Nothing happens when rows are selected and onRowSelected is usedShort 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.
Fix it fast
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)} />2Match what you see, then jump to its fix
What does the error look like?
Nothing happens when rows are selected and onRowSelected is usedmodel.map is not a function on the selection valueThe 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 browserConfirmed by mui.com
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 checkboxConfirmed by mui.com
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
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
The MUI X Data Grid ignores or rejects getRowId
Selection or updates affect the wrong rows
The Data Grid reads each row's id from a property called id by default.
3 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fix