Error trying to fetch data from an API which is then passed to a DataGrid.
The data is returned as follows:
{
data: [{
type: 'PropertyDamage',
id: '100',
attributes: {
identification_number: '3931',
damage_date: '2021-04-29',
report_date: '2021-06-26'
}
},
{
type: 'PropertyDamage',
id: '101',
attributes: {
identification_number: '3839',
damage_date: '2021-01-21',
report_date: '2021-08-25'
}
},
{
type: 'PropertyDamage',
id: '102',
attributes: {
identification_number: '3735',
damage_date: '2021-04-25',
report_date: '2021-10-29'
}
}
]
}
The component that is handling the fetching of data as well as rendering the DataGrid:
const columns = [
{ field: "id", headerName: "ID" },
{ field: "type", headerName: "TYPE" },
];
const Dashboard = () => {
const [propertydamages, setPropertyDamages] = useState([]);
useEffect(() => {
const url = "URL";
fetch(url, {
method: "GET",
withCredentials: true,
headers: {
"X-API-Key": "API Key",
},
})
.then((response) => response.json())
.then((json) => {
setPropertyDamages(json);
})
.catch(function (error) {
console.log(error);
});
}, []);
return (
<Box m="20px">
{/* Data Grid */}
<DataGrid rows={propertydamages} columns={columns} />
</Box>
);
};
The error being logged to the console on render:
index.js:1272 Warning: Failed prop type: Invalid prop `rows` of type `object` supplied to `ForwardRef(DataGrid)`, expected an array.
at div
at http://localhost:3000/static/js/0.chunk.js:942:73
at Box (http://localhost:3000/static/js/0.chunk.js:69597:74)
at div
at http://localhost:3000/static/js/0.chunk.js:942:73
at Box (http://localhost:3000/static/js/0.chunk.js:69597:74)
at Dashboard (http://localhost:3000/static/js/main.chunk.js:373:77)
at RenderedRoute (http://localhost:3000/static/js/0.chunk.js:153998:27)
at Routes (http://localhost:3000/static/js/0.chunk.js:154437:24)
at main
at div
at InnerThemeProvider (http://localhost:3000/static/js/0.chunk.js:68080:72)
at ThemeProvider (http://localhost:3000/static/js/0.chunk.js:67042:24)
at ThemeProvider (http://localhost:3000/static/js/0.chunk.js:68098:24)
at App (http://localhost:3000/static/js/main.chunk.js:51:72)
at Router (http://localhost:3000/static/js/0.chunk.js:154367:30)
at BrowserRouter (http://localhost:3000/static/js/0.chunk.js:152656:23)
I tried changing the data into different formats but it was not working.
useEffectdependency array. To me it looks like it's in the wrong place. It should come after the next{.useEffect(( )=>{},[]). You may be in an infinite looptype object supplied to ForwardRef(DataGrid), expected an array.Best to check that your data is an array and not something like{results:[//data]}rowsof typeobjectsupplied toForwardRef(DataGrid), expected an array. This is the error