20

I'm using meterial-table with React. I'm trying to assign data from an array coming from an api like this

<MaterialTable
  columns={columns}
  data={rows}
  ...
/>

Where columns and rows are api data. But I'm getting this error:

TypeError: Cannot add property tableData, object is not extensible

Notably When I use mock hard-coded data, things are working perfectly. After some search, I couldn't find any solution for it, any help?

J. Doe 2018
  • 313
  • 3
  • 8
  • 1
    https://github.com/apollographql/react-apollo/issues/1251 – Anatsu Jan 08 '20 at 15:00
  • related : https://stackoverflow.com/questions/55567386/react-cannot-add-property-x-object-is-not-extensible - and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isExtensible , Found this thread as I am implementing material-table using redux, it works in my storybook story as I use the mocked data directly, but in development I use the mocked data via the redux store and got the same error. – JimiSweden Mar 02 '20 at 11:03

5 Answers5

26

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?

Siraj Kakeh
  • 741
  • 6
  • 20
  • 1
    worth noting if using this approach, "you only get shallow copies", it might be what you want, or it might not =) https://2ality.com/2016/10/rest-spread-properties.html#pitfall%3A-cloning-is-always-shallow – JimiSweden Mar 02 '20 at 13:13
24

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.
Tobias Bergkvist
  • 1,751
  • 16
  • 20
  • 3
    Thank you very much for your suggestion/explanation. I had exactly the same problem using `immer`. Thankfully, `immer` indeed provides a functionality to disable freezing: `import { setAutoFreeze } from 'immer'; setAutoFreeze(false);` – guid May 05 '20 at 16:14
3

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

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

yonadav bar ilan
  • 550
  • 6
  • 10
3

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.

Shubham Dixit
  • 9,242
  • 4
  • 27
  • 46
  • fwiw, you should put that: JSON.parse(JSON.stringify(useSelector(state=>state.cycleSlice))) into its own selector. That way it will be cached (memoized). – JohnFlux Feb 06 '22 at 02:35
2

In my case Using "structuredClone" function solved this same issue.

var a = useSelector((state) =>state.cusLocChartTable.CusLocationCT);
const cloneData = structuredClone(a.billcity_table);

<ThemeProvider theme={defaultMaterialTheme}>
  <MaterialTable
       columns={[
               { title: 'City', field: 'city' },
               { title: 'Customers', field: 'customers' }
               ]}
               data={cloneData}
               title="Customers"
  />
</ThemeProvider>
sam707
  • 75
  • 1
  • 6