I have a product and i want to get the total price of this product based on quantity, I'm using zustand for state management.
here's a function I used to update the state.
export const useCartProduct = create<CartProductsProp>(
devtools(
persist(
(set) => ({
incrementProductQuantity: (product: ProductProps) => {
set((prev) => {
console.log('@userQuantity-product', product.userQuantity);
let currentCart = prev.cartProducts.map((cartProduct) =>
cartProduct.id === product.id
? {
...cartProduct,
userQuantity: cartProduct.userQuantity + 1,
productTotalPrice: cartProduct.userQuantity * cartProduct.price, // here's the issue "the userQuantity not updated and return the prev value".
}
: cartProduct,
);
return {
cartProducts: currentCart,
};
});
},
}),
),
),
);
the issue here
productTotalPrice: cartProduct.userQuantity * cartProduct.price
"the userQuantity
not updated and return the prev value".
So what I tried is like this
productTotalPrice: (cartProduct.userQuantity + 1) * cartProduct.price,
that's return the correct quantity 'updated one', So is others way to fix it?