I'm making a shopping cart app and I have a variable that loops through all prices and gets a total price of the cart. I am trying to have that total amount update the totals state through setTotals but while the total variable itself updates as items are removed from the cart, calling setTotals(total) does not update the state. It stays with the initial state on render (total price of all prices in cart).
my state declaration:
const [totals, setTotals] = useState()
getting the total from prices array:
var total = 0
for (var i = 0; i < prices.length; i++) {
total += prices[i];
}
calling setTotals in useEffect:
useEffect(() => {
setTotals(total) <-- From the loop above
setCartCopy([...deepCartCopy])
}, [totals])
Can anyone please help?
useEffect
dependencies should be, [total])
singular – danihazlertotal
is not a state then it should not be dependencies – monesul haque