I have updateCart function. I want to run it when we reload the page and Cart state have some value. As we know useState hook renders two times on reload and empty on the first render and updates with values on the second render. I want this function run when state have some values i.e, on second render.
const updateCart = () => {
let newCart = { ...Cart }
let keys = Object.keys(Cart)
for (let i = 0; i < keys.length; i++) {
let previousPrice = Cart[keys[i]].price
let currentPrice = allproducts[0].products.find(x => x.sku == keys[i]).product_price
if (previousPrice != currentPrice) {
newCart[keys[i]].price = currentPrice
}
}
updateNewCart(newCart)
}
useEffect(() => {
if (Object.keys(Cart).length != 0) {
updateCart()
}
}, [])