just have a question about how to push onto an array to ultimately store in the localstorage. I have the below code:
const handleSelectLayouts = (layout) => {
const layoutsArray = [];
layoutsArray.includes(layout)
? layoutsArray.filter((str) => str !== layout)
: layoutsArray.push(layout);
localStorage.setItem('layouts', JSON.stringify([...layoutsArray]));
console.log(layoutsArray)
}
I see it in localstorage, however, it only has one item at a time. In the code, I am trying to push onto an array. Not just have whatever is the most recent item inside the array alone. Anyone see anything odd here?
2
Answers
You need to retrieve the previously stored layoutsArray from the local storage
You are defining a new layoutsArray every time the function is called
If you want to add to the array which is already in localStorage then try this