use-shopping-cart logouse-shopping-cart

incrementItem()

Increases the quantity of an item already in the cart by 1 (or by a custom amount).

Basic Usage

import { useShoppingCart } from 'use-shopping-cart'

function CartItem({ itemId }: { itemId: string }) {
  const { incrementItem, cartDetails } = useShoppingCart()
  const item = cartDetails[itemId]

  return (
    <div className="cart-item">
      <span>{item.name}</span>
      <span>Quantity: {item.quantity}</span>

      <button onClick={() => incrementItem(itemId)}>+1</button>
    </div>
  )
}

Increment by Custom Amount

You can increase the quantity by more than 1 (positive integers only):

// Increase by 10
incrementItem(itemId, { count: 10 })

Complete Cart Example

function Cart() {
  const { incrementItem, cartDetails, cartCount } = useShoppingCart()

  if (cartCount === 0) {
    return <p>Your cart is empty</p>
  }

  return (
    <div>
      {Object.entries(cartDetails).map(([id, item]) => (
        <div key={id} className="cart-item">
          <h4>{item.name}</h4>
          <p>Quantity: {item.quantity}</p>

          <button onClick={() => incrementItem(id)}>+</button>
          <button onClick={() => incrementItem(id, { count: 5 })}>+5</button>
        </div>
      ))}
    </div>
  )
}

Interactive Demo

Cart Display

Total Items: 0

Cart is empty. Add items to see them here!

Bananas product photo

Bananas

Yummy yellow fruit

$4.00

Add to Cart First:

Add the item to your cart to try incrementing

Try incrementing items above! The cart state persists across all documentation pages.

Shopping Cart

Your cart is empty

Try the interactive demos on the docs pages!