Im using Material UI's datagrid to show some data in a table, I recently switched over to RTK query from redux for caching and to get rid of the global state management in the project but encountered a problem as mentioned in the title.
The error that I'm currently seeing is TypeError: Cannot read properties of undefined (reading 'name')
This occurs when trying to get data in a nested object, I'm using valueGetter to get to the nested value. The const below shows the columns of the table and where valueGetter is being used (I've removed some of the columns for this example)
const columns = [
{ field: "id", headerName: "ID", width: 70 },
{
field: "App",
headerName: "App",
width: 110,
valueGetter: (params) => params.row.app.name, // <---- Recieving error here
valueFormatter: (params) => capitalise(params.value),
}
];
This is what my component looks like (I've also removed some of the code just for this example):
import React, { useEffect, useState } from "react";
import { DataGrid } from "@mui/x-data-grid";
import { IconButton } from "@material-ui/core";
import { CustomLoadingOverlay } from "../gbl/datagrid/DataGridUtils";
import { capitalise } from "../utils/util";
import {
useGetAppAccountTypesQuery,
useDeleteAccountTypeMutation,
} from "../../services/reducers/accounttypes";
export default ({ appid }) => {
const { data, isLoading, isSuccess } = useGetAppAccountTypesQuery(appid);
const [loading, setLoading] = useState(true);
console.log(data, isLoading, isSuccess);
// Testing only
useEffect(() => {
if (isSuccess) setLoading(false);
}, [isLoading]);
const columns = [
{ field: "id", headerName: "ID", width: 70 },
{
field: "App",
headerName: "App",
width: 110,
valueGetter: (params) => params.row.app.name,
valueFormatter: (params) => capitalise(params.value),
}
];
return (
<>
<div
style={{
height: 190,
width: "100%",
backgroundColor: "white",
marginTop: 40,
borderRadius: 4,
}}
>
<DataGrid
loading={loading}
rows={data}
columns={columns}
components={{
LoadingOverlay: CustomLoadingOverlay,
}}
hideFooter
density="compact"
pageSize={6}
rowsPerPageOptions={[5]}
checkboxSelection
disableSelectionOnClick
></DataGrid>
</div>
</>
);
};
The component re-renders a couple of times, from each render the console.log(data, isLoading, isSuccess) looks like
undefined true false <---- this gets logged 3 times
Array(2) false true
Array(2) false true
this issue seems to be intermittent as it does sometimes work,
Before switching to RTK query, I was using Axios and useEffect to call the endpoints and never received this error, am I doing something wrong here?
Any help would be appreciated.