Maaz Zindani

Loading the index…

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

MUI DataGrid warns that rows is an object when it expects an array

ErrorWarning: Failed prop type: Invalid prop `rows` of type `object` supplied to `ForwardRef(DataGrid)`, expected `array`.

Short answer

The DataGrid rows prop expects an array of objects. The warning appears when you pass something else that typeof reports as object: the whole API response instead of its list, an object keyed by id, or a promise from an async call. Pass the array itself, and keep its reference stable between renders.

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

Fix it fast

  1. 1Find out which cause you have

    Print what you pass to rows, with its type, right before the grid.

    console.log(Array.isArray(rows), typeof rows, rows);
  2. 2Match what you see, then jump to its fix

    1. 1The log shows false, 'object', and a value like { data: [...], total: 40 }The whole response object is passed instead of the list inside itFix
    2. 2The log shows an object like { '1': {...}, '2': {...} }The data is an object keyed by idFix
    3. 3The log prints Promise { <pending> }A promise or an async function result is passedFix

What does the error look like?

  • Warning: Failed prop type: Invalid prop `rows` of type `object` supplied to `ForwardRef(DataGrid)`, expected `array`.

Cause 1 of 3

The whole response object is passed instead of the list inside it

1Check

Many APIs wrap the list, and axios adds its own wrapper, so the array sits one or two levels down.

console.log(response.data);

2Fix

Read the array out of the response and give the grid that. Start the state as an empty array so the first render is valid.

const [rows, setRows] = useState([]);

useEffect(() => {
  axios.get('/api/users').then((res) => setRows(res.data.items));
}, []);

<DataGrid rows={rows} columns={columns} />

3Verify

The warning disappears and the grid shows the rows.

console.log(Array.isArray(rows)); // true

Confirmed by mui.com

Did this fix it?

Cause 2 of 3

The data is an object keyed by id

1Check

Stores that keep records by id (normalised state) give you a map, and the grid wants a list. The same page says each row needs an id.

console.log(Object.keys(rows).slice(0, 3));

2Fix

Convert the map to an array with Object.values. If the id is not stored in the record, copy it in, or tell the grid where it is with getRowId.

const list = useMemo(() => Object.values(rowsById), [rowsById]);

<DataGrid rows={list} columns={columns} getRowId={(row) => row.uid} />

3Verify

The warning is gone and each row appears once.

console.log(Array.isArray(list)); // true

Confirmed by mui.com

Did this fix it?

Cause 3 of 3

A promise or an async function result is passed

1Check

An async function always returns a promise, which is an object. Passing its call result to rows hands the grid the promise.

console.log(rows instanceof Promise);

2Fix

Await the call inside an effect, store the resolved array in state, and pass that state.

useEffect(() => {
  let ignore = false;
  loadRows().then((data) => { if (!ignore) setRows(data); });
  return () => { ignore = true; };
}, []);

3Verify

rows is an array on every render and the warning no longer prints.

console.log(Array.isArray(rows)); // true

Confirmed by mui.com

Did this fix it?

How do I stop it coming back?

  • Log or assert Array.isArray(rows) once while building the screen, before you wire the grid.
  • Initialise table state with [] and never with null or an empty object.
  • Keep the rows reference stable between renders, as the docs ask; build derived arrays with useMemo.

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.