I am new to the Reactjs development side and to understand and have a complete understanding of reactjs i have started working on the ecommerce application. I have finished with the display of products page with the help of .JSON file. So with that i used those id's of the products and i was able add the particular item to the cart and remove them.
The problems I am unable work with are:
Help needed with
For reference Github link for code: https://github.com/BhupathiVenkataSaiCharan/reactredux/tree/master/src/Redirects/Fvdata
In Fvdata, I have my list of products in data.json In Fvdata > App.js , I have the use of click functions for Adding and removing the cart items.
I check your code and from what I understand you have to tweak these two methods on your App.js file
removeFromCart=(product)=>{
const cartItems = this.state.cartItems.slice();
this.setState({
cartItems: cartItems.filter(x=>x.id !== product.id),
});
localStorage.setItem("cartItems",JSON.stringify(cartItems.filter(x=>x.id !==product.id)));
}
clearCart=()=>{
const cartItems = this.state.cartItems.slice();
this.setState({
cartItems: cartItems.filter()
})
}
into this
removeFromCart=(product)=>{
const cartItems = this.state.cartItems.slice().map(x => {
if(x.id === product.id){
return {
...x,
count: x.count -1
}
}
return x
})filter(x=>x.count > 0);
this.setState({cartItems});
localStorage.setItem("cartItems",JSON.stringify(cartItems));
}
clearCart=()=>{
this.setState({
cartItems: []
})
localStorage.setItem("cartItems",JSON.stringify([]));
}```