Can someone give me a light on what may be happening here? I have an effect that basically, when I load a product page it updates an state with an array of items, but when I change the product (a prop) it's not cleaning up my state. The code is below, can someone see what may be missing?
const [cartItems, setCartItems] = useState<any>([]);
useEffect(() => {
if (product) {
handleCartArray(product.name, variant.sku, variant.price, product.productId);
}
return () => {
console.log('is this running?');
cleanCartItems();
};
}, [product]);
function cleanCartItems() {
console.log('I am trying to clean the cart items');
setCartItems([]);
}
Not really much to be added here. I am trying to clean up this state everytime a new product is loaded, but it isn't working.