score:18

Accepted answer

this has nothing to do with material-table or react. most probably this is related to your api response having object.preventextensions() applied on it for some reason, maybe this is an axios behavior. so when material-table is trying to add an id field to each object, it's facing this error. although not optimal, try to copy your api data to a new array of objects so material-table can modify them, e.g:

const editable = rows.map(o => ({ ...o }));
<materialtable
  columns={columns}
  data={editable}
  ...
/>

note that i didn't use rows.map(o => o) as this will copy the array with the same objects references

edit: it's worth mentioning that using spread operator or object.assign will only give a shallow copy, i.e. will not copy nested objects. one work-around for this is to use json.parse(json.stringify(object)). please note that this would cause some data loss, other alternatives are on this answer: what is the most efficient way to deep clone an object in javascript?

score:0

i got this error while passing the array data in the material data table i was using reduxjs/toolkit

as the object are not modifiable ,due to internal implementation of object.freeze() by

reduxjs/toolkit

 const {cyclelist}=json.parse(json.stringify(useselector(state=>state.cycleslice))); 

i used above method to create a new copy of the object.

score:3

import { setautofreeze } from 'immer'; setautofreeze(false);

worked for me. material table should consider an api that plays well with immer

score:21

you are most likely using immer or a library that uses immer under the hood (like @reduxjs/toolkit). immer uses object.freeze to make the objects it produces immutable.

material-table modifies its own props (which is a very ugly antipattern). when libraries break rules, they won't work with libraries that try to enforce them.

there is no way to unfreeze an object that has been frozen, but you have a couple of options:

  1. find a way to disable freezing in the immer instance (check out the api docs of whatever you think might have frozen your state).

  2. override object.freeze making it do nothing (very hacky, should be avoided - and yet it might be your best shot here):

window.object.freeze = function(obj) { return obj }
  1. clone/deep copy your state before passing it to materialtable. this is also far from ideal, especially if you have a lot of data.

Related Query

More Query from same tag