If you mutate the state directly it won't re-render.
Instead of...
[cart, setCart] = useState([1,2,3])
const updateCart = (item) => {
prevCart = cart
prevCart.push(item)
setCart(prevCart)
}
Do this....
const updateCart = (item) => {
let prevCart = []
cart.forEach(item => {
prevCart.push(item)
});
setCart(prevCart)
}
Instead of...
[cart, setCart] = useState([1,2,3])
const updateCart = (item) => {
prevCart = cart
prevCart.push(item)
setCart(prevCart)
}
Do this....
const updateCart = (item) => {
let prevCart = []
cart.forEach(item => {
prevCart.push(item)
});
setCart(prevCart)
}
Comments
Post a Comment