I have a simple setup for my Mui DataGrid. The issue I have though, is that renderCell
isn't rendering my elements conditionally.
It should show per default an EditIcon
button (the pencil) and as soon as I click on it, it should call fetchSomething
- which it does - and switch to the Mui CircularProgress
element by setting isLoading
to true but the CircularProgress element is never displayed.
I tested quickly via a useEffect
to see if the state changes and it does change from false to true then again to false but the CircularProgress
is nowhere to be seen.
Am I having issues with ids? Any way to solve this?
const Component = () => {
const [isLoading, setIsLoading] = useState(false);
const fetchSomething = (event) => {
setIsLoading(true);
fetch(event.id)
.then(() => doSmth())
.catch((error) => console.log(error))
.finally(() => setIsLoading(false));
};
const usersTableColumns = [
{
field: 'id',
headerName: 'ID',
width: 200,
flex: 1,
},
{
field: 'username',
headerName: 'USER NAME',
width: 170,
flex: 1,
},
{
field: 'action',
headerName: 'Edit',
sortable: false,
renderCell: (params) => (isLoading ? <CircularProgress size={30} /> : (
<EditIcon onClick={fetchSomething} />
)),
},
];
return(
<DataGrid rows={rows} columns={usersTableColumns}/>
);
}
EDIT
According to the logic above all pencils should become spinners as soon as I click on one of them, so I tried adding dynamically the ids
to the state and checking for a certain id
when displaying CircularProgress
but it seems like the state updates just don't show up for renderCell
.