Reputation: 67
I'm working with react native woocommerce API to build an android app. I have completed all major functionalities but I can't remove all products from the cart when I check out is successful.
const { cart } = this.props;
this props consist of name,image,qty,price and produc id of the products.
I tried
const { cart } = this.props;
cart.forEach(function(element) {
delete element.id;
delete element.price
//and all elements
});
this deletes the property but when I open cart page this shows undefined object error. how to remove the object cart itself.
Upvotes: 0
Views: 2899
Reputation: 1833
The props in react are readonly which means they cannot be deleted, however you can achieve the functionality you want to implement by setting cart prop in the state and then updating the state. You can do following:
constructor(props){
super(props);
this.state = {
cart: props.cart
}
}
and then you can use setState to update the state.
this.setState({cart: UpdatedCart}):
Upvotes: 1
Reputation: 703
you can't delete props in react. The props of a react component are immutable and are not supposed to be changed by the component.
If you need to work with the data locally, you could use the state of the component, or better create a local copy of the prop data.
Upvotes: 0