0

So I'm working on a project where I want to have a shopping cart and inside of that shopping cart you can see the product's quantity and change it by clicking the buttons on the side of it. The problem is that the value of the quantity updated but it is not shown in the shopping cart and it doesn't seem to be taking an effect on the price at all. The quantity is being stored as an object and is updated in functions called upon pressing the buttons. Here's the releveant code: Those are my decrease and icnrease functions:

 const increaseQuantity=(product)=>{
        product.quantity+=1;


    }
    const decreaseQuantity=(product)=>{
        if(product.quantity<=1){
           alert('ERROR')
        }
        if(product.quantity>=1){
            product.quantity-=1;

        }

    }

and that is where I show the price within the map function:

{productsInCart.map(product=>    
            <div className='productsWrapper'>
                <p className='productName'>{product.name}</p>
                <img className='productImage' src={product.image}/>
                <p className='productPrice'>
                    <span className='productQuantity'>
                        <FaArrowLeft className='decreaseQuantity' size={20} onClick={()=>decreaseQuantity(product)}/>
                            <span className='quantity'>
                                {product.quantity}
                            </span>
                        <FaArrowRight className='increaseQuantity' size={20} onClick={()=>increaseQuantity(product)}/>
                    </span> Total: ${product.price*product.quantity}</p>
                <FaTrash className='removeProduct' size={25} onClick={()=>removeFromCart()}></FaTrash>
                <hr></hr>
                <span className='calculateTotal'>{totalSumDollars+=product.price}</span>       
            </div>)
            }

And this is where my products are being stored:

    const productssList=[
    {
        name: 'Product 2',
        image: MissingImage,
        price: 5.50,
        quantity: 1,
        id: 0
    },
    {
        name: 'Product 1',
        image: MissingImage,
        price: 3.40,
        quantity: 1,
        id: 1
    },
2
  • Does the quantity update here <span className='quantity'> {product.quantity} </span> when you increase and decrease? Commented Nov 24, 2020 at 7:23
  • No it doesn't update, it stays at the default value of 1 Commented Nov 24, 2020 at 7:24

2 Answers 2

1

You are not updating the productsInCart object when you are increasing and decreasing quantity. You need to update the quantity in productsInCart. See below code.

return cartItems.map((cartItem) =>
  cartItem.id === cartItemToAdd.id
    ? { ...cartItem, quantity: cartItem.quantity + 1 }
    : cartItem
);

For your case.

const increaseQuantity=(product)=>{
        return productsInCart.map((cartItem) =>
          cartItem.id === product.id
          ? { ...cartItem, quantity: cartItem.quantity + 1 }
          : cartItem
  );
}
Sign up to request clarification or add additional context in comments.

Comments

0

I think you should go and use useState hook here. https://reactjs.org/docs/hooks-state.html. If you are already using the useState hook, make sure you immutable the array with spread or other methods.

2 Comments

I've thought of that but I am using a class component, I'm not sure if i can use hooks within class components
If you are using a class component you can use the state and then dont forget that you immutable the array with spread or other methods reactjs.org/docs/state-and-lifecycle.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.