Currently, I am working on a project using Webpack in vsCode where I am storing objects in an array named taskList and saving it to localStorage. Whenever a user creates a new task, a new object called newAddition is created and added to the taskList array retrieved from localStorage on form submission (click event). The process works perfectly while the site is open; however, upon closing and restarting vsCode, my array disappears from localStorage.
const taskList = JSON.parse(window.localStorage.getItem("taskList"));
taskList.push(newAddition);
window.localStorage.setItem("taskList", JSON.stringify(taskList));
In my index.js file in Webpack, I have written code to handle retrieving taskList from localStorage or creating a new blank array if it doesn't exist on the first visit. Despite this setup, every time I restart vsCode, my array vanishes. Interestingly, if vsCode remains open and Firefox is restarted, the localStorage data persists. Can someone help me identify what might be going wrong?
const taskList = JSON.parse(window.localStorage.getItem("taskList"));
if (taskList === null) {
const taskListcreate = [];
localStorage.setItem("taskList", JSON.stringify(taskListcreate));
}
I'm wondering if there could be something in the default Webpack configuration (or webpack.config.js or package.json) causing the local storage to be cleared with each application restart. I cannot think of any changes that would lead to this behavior, but it seems like the only possible explanation for the data loss.