Hello everyone, I am having a small issue with my filter method and setting state.
my method looks like this
Removefromcart = ({ title, desc, price, image, id }) => {
this.setState(state => ({
cart: this.state.cart.filter(item => item.id !== id),
//total: state.total - price
}));
};
At the moment this method erases everything in the cart array.
my items look like this
this.state.items.map(({title, imageUrl, desc, price, id }) => (
<Card key={id} id={id} style={{ width: '18rem'}}>
<Card.Img variant="top" src={imageUrl} width='10rem' height='180rem' />
<Card.Body>
<Card.Title>{title}</Card.Title>
<Card.Text>
{desc} <br />
{price}
</Card.Text>
<Button variant="primary" onClick={e => this.handleAddToCart(e, { title, image : imageUrl, desc, price })}>Add to cart</Button>
</Card.Body>
</Card>
I’m sure there’s just a simple mistake I am making, but I can’t figure it out.
Thank you in advance for your help and expertise.