Advertisement
Guest User

Untitled

a guest
Jul 17th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. handleClick = (product) => {
  2. product['quantity'] = 1;
  3. let sameItem = this.state.cartItems.findIndex(item => item.id == product.id);
  4. console.log(sameItem);
  5. if(sameItem != -1){
  6. if(this.state.cartItems[sameItem].quantity < 10){
  7. this.state.cartItems[sameItem].quantity += 1;
  8. }
  9. } else {
  10. let cartItems = [...this.state.cartItems, product];
  11. this.setState({ cartItems }, () => console.log(this.state.cartItems));
  12. }
  13. console.log(this.state.cartItems);
  14.  
  15.  
  16. }
  17.  
  18. {/* This is all im trying to display with the cart for now, but it does not increment, but even in the console it does not increment */}
  19. <div>
  20. {cartItems.map(item => (
  21. <p key={item.id} >{item.quantity}</p>
  22. ))}
  23. </div>
  24.  
  25. if(this.state.cartItems[sameItem].quantity < 10){
  26. this.state.cartItems[sameItem].quantity += 1;
  27. this.setState({cartItems});
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement