0

i've got stuck in react problems. I've create an online shop (front-end) website in react

const products = [
  { id: 1, name: "Oppo", price: 200, unit: 1, img: "oppo.jpg" },
  { id: 2, name: "Samsung", price: 250, unit: 1, img: "samsung.jpg" },
  { id: 3, name: "Vivo", price: 150, unit: 1, img: "vivo.jpg" },
];

/* const addToCart = (items) => { 
    setCartItem((cartItem) => [...cartItem, items])
    console.log(cartItem)
  }

function to add data in array to cartItem state

 */

const [cartItem, setCartItem] = usestate([]);

const addQuantity = (id) => {
  const updateItem = cartItem.map((item) => {
    if (item.id === id) {
      return { ...item, unit: item.unit + 1, price: unit * item.price };
    }
    return item;
  });
  setCartItem(updateItem);
};

const reduceQuantity = (id) => {
  const updateItem = cartItem.map((item) => {
    const updateUnit = item.unit - 1;
    if (item.id === id && item.unit > 1) {
      return { ...item, unit: updateUnit, price: item.price / item.unit };
    }
    return item;
  });
  setCartItem(updateItem);
};

<div>
  {cartItem.map((item) => {
    return (
      <div>
        <button onClick={() => addQuantity(item.id)}>+</button>
        <img src={item.IMG} alt="" />
        <h3 className="name">{item.product}</h3>
        <p className="price">{item.price}</p>
        <p className="quantity">{quantity}</p>
        <button onClick={() => reduceQuantity(item.id)}>-</button>
      </div>
    );
  })}
</div>;

the initial unit of vivo is 1 and the price is 150 when I click addQuantity button, the unit has become 2, but because of the initial unit is 1, the price is still 150 (1 * 150). I add again, the unit is 3 and the price is 300 ( 2 * 150 ) where it should be 450.

how to keep the initial value not changes so the the function gives the correct result?

2
  • Why not make declare the products array using useeState to make it reactive Commented Jun 12, 2024 at 23:02
  • sorry, i'm confuse. The products array is to strore all the data to display some product in the homepage, and the cartItem state to store some products which user add to cart Commented Jun 13, 2024 at 21:36

1 Answer 1

0

You are returning { ...item, unit: item.unit + 1, price: unit * item.price }; that means you are adding one to unit property but you are not adding it when calculating price.

Let's say that item.unit = 3, which means unit: 3 + 1, price: 3 * item.price while it should be price: (3+1) * item.price.

Also, I think that you have a typo and price: unit * item.price should be price: item.unit * item.price.

That said, you need to add one also when calculating price { ...item, unit: item.unit + 1, price: (item.unit + 1) * item.price };

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for correcting my code. i've solved this problem with creating a function const calcPrice = (quantity, price) => { return quantity * price } and put it on price element <p className="total-items-price">${calcPrice(item.unit, item.price)}</p> . the value of price and unit are not changing and i get the solution, but i dont know that react can render some functions like that. i'm realy new with react js somehow its worked

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.