I have tried implementing and adding data into local storage and it does not adding instead it keeps on updating the old one .can anyone say that how to add array of objects entered by user , so that it can be stored in the localstorage in react js?
Asked
Active
Viewed 2,016 times
0
-
localStorage.setItem(' [key] ',JSON.stringify(' [value] ')); – Bilal Mohammad Aug 28 '21 at 13:16
-
1Does this answer your question? [Storing Objects in HTML5 localStorage](https://stackoverflow.com/questions/2010892/storing-objects-in-html5-localstorage) – Giorgi Gvimradze Aug 28 '21 at 13:19
-
Please provide enough code so others can better understand or reproduce the problem. – Community Aug 30 '21 at 15:13
1 Answers
2
You'll have to set the whole object at once;
const items = JSON.parse(localStorage.getItem("myItems"));
const newItems = JSON.stringify([...items,{name:"new item"}])
localStorage.setItem("myItems",newItems);

Anuja
- 908
- 6
- 11