I am trying to save an array of strings in localStorage. This is my code:
export const saveComment = (state) => {
try {
const commentExisting = localStorage.getItem("comment");
const comment = commentExisting ? commentExisting.split(", ") : [];
comment.push(state);
localStorage.setItem("comment", JSON.stringify(comment));
} catch (err) {
console.log(err);
}
};
After adding a couple of values I get a lot of escape characters:
["[\"[\\\"Some comment\\\"]\",\"another one\"]","one more comment"]
I want it to be like this: ["some comment", "another comment", "one more comment"]
What is wrong with my code?