I'm making an advent calendar in React and using localStorage so the opened doors remain open when the user return the next day.
I am able to write to and read the localStorage but the first time the user clicks on the door it is not added, it adds it to the openedArr array but not localStorage, I'm sure I'm missing something simple but cannot see it! here is my code..
const localData = localStorage.getItem("openDoors");
const [openedArr, setOpenedArr] = useState(localData ? localData : []);
const openDoor = e => {
e.currentTarget.parentElement.classList.add("opened");
setOpenedArr([...openedArr, e.currentTarget.id]);
localStorage.setItem("openDoors", openedArr);
};