const [storageValue, setStorageValue] = useState(() => JSON.parse(localStorage.getItem('productCardsId')) || []);
const localStoreHandler = productId => {
setStorageValue(prevValue => [productId, prevValue?.[0]]);
localStorage.setItem('productCardsId', JSON.stringify(storageValue));
};
When I set setStorageValue with a first value I only get an empty array. After the second click, I get the value but delayed. How I can update the state on the first click? Thank you.