Home > Mobile >  React Native: cannot assign to read-only property error when trying to modify an array from another
React Native: cannot assign to read-only property error when trying to modify an array from another

Time:10-22

I have a folder named 'data' where I keep different arrays in each file and export them by default, I then get the data from these arrays and display them in the app. However, If I try to modify them from another component, I get the aforementioned error. In this case:

// './data/array'
array = []
export default array;

// './screens/component'
import array from './data/array'
array.push(x)

raises the error.

CodePudding user response:

Wrap it into an object, e.g.

const MyObject {
   const array = [];
};

export default MyObject;

I believe it should also work with a non-default export:

export const array = [];
  • Related