I'm getting this error -
Cannot add property Quantity, object is not extensible
in Files.jsx
itemsInCart array of objects is defined at the App.jsx and getting filled in other jsx component, I checked with console.
App.jsx
const ItemsCartWithLocalStorage = (userItems) => {
const itemsInCart = useRef(
JSON.parse(localStorage.getItem(userItems)) || new Array());
useEffect(() => {
console.log('localStorage setItem');
localStorage.setItem(userItems, JSON.stringify(itemsInCart));
}, [itemsInCart]);
return [itemsInCart];
};
function App() {
const itemsInCart = useRef(ItemsCartWithLocalStorage('itemsQuantityString'));
return (
<Files itemsInCart={itemsInCart} />
)
}
Files.jsx
const Files= ({ itemsInCart }) => {
const array = [...itemsInCart.current];
for (let i = 0; i < array.length; i++) {
array[i]['Quantity'] = 1; "TypeError: Cannot add property Quantity, object is not extensible"
How can I fix this error ?