Vercel Logo

fingertipps

Showcase

Decreasing Cart Item count

Import the "decreaseItemCount" function from the fingertipps-handshakes package. pass in the cart item you want it's count decreased here is our implementation of this


import { decreaseItemCount } from "fingertipps-handshakes";
const CartPage = () => {
  const updateCartCount = (count) => {
    console.log(count);
    // code to update your ui
  };

  return (
    <div>
      {itemsInCart
        ? itemsInCart.map((item) => (
            <div key={item._id}>
              <div>
                <img src={item.photo[0].picture} alt="" />
              </div>
              <p>{item.name}</p>
              <p>₦{item.price.toLocaleString()}</p>
              <p>{item.count.reduce((a, c) => a + c, 0).toLocaleString()}</p>
              <button
                onClick={() =>
                  decreaseItemCount(item, updateCartCount) & UiUpdate()
                }
              >
                -
              </button>
            </div>
          ))
        : ""}
    </div>
  );
};

export default CartPage;

In the code snippet above, we are iterating through an array of objects that we received from the "getCartItems" function. Whenever the user clicks the increase button for an item, that specific item is passed to the "decreaseItemCount" function. As a result, the count of that item is automatically decreased.