I am using localStorage
to add an array of objects to it. And it works fine. But it works 1 step late.
For example: i make a click on a button and nothing is entered into localStorage
, but as soon as i make a second click, i get the entry from the first click and so on.
I know that state
is asynchronous., that’s why I put localStorage.setItem
inside state
:
const AddSymbol = (symbol) => {
setCustomArray((prev) => {
localStorage.setItem("ItemCustomArray", JSON.stringify(CustomArray));
return Object.assign([], prev, {
[index]: {
symbols: [...prev[index].symbols, symbol]
}
})
})
}
But that didn’t help. Please. can you tell me what is the problem? Thanks!