I am developing an web app where the user clicks on a row in a table and it turns green then saves it into localstorage so that every time they access the web page it will save which row they have clicked on. I have this weird problem when I try to save my value to React localstorage the value empties. I have to use JSON.stringify to be able to save it to localstorage. Below is my code
var store = JSON.parse(localStorage.getItem("name"));
if (store === null) {
store = new Set();
} else {
store = new Set(store);
}
function saveStore() {
localStorage.setItem("name", JSON.stringify([...store]));
}
const handleRowClicked = row => {
const updatedData = data.map(item => {
if (row.uid !== item.uid) {
return item;
}
console.log(item.uid)
if(store.has(item.uid)) {
store.delete(item.uid);
} else {
store.add(item.uid);
}
saveStore();
return {
...item,
toggleSelected: !item.toggleSelected
};
});
setData(updatedData);
};
Ive tried printing the value of the stringify method and it always comes out exactly what I want it to but when I check the localstorage using the dev console it is always empty. I have also printed in the if cases so I know that code is executing the correct statement. It should add it to store then go the saveStore() method which will stringify the store set and save it into localstorage. Can someone help me?