removeItem()
Completely removes a product from the cart, regardless of quantity.
Basic Usage
import { useShoppingCart } from 'use-shopping-cart'
function CartItem({ itemId }: { itemId: string }) {
const { removeItem, cartDetails } = useShoppingCart()
const item = cartDetails[itemId]
return (
<div className="cart-item">
<span>{item.name}</span>
<span>Quantity: {item.quantity}</span>
<button onClick={() => removeItem(itemId)}>Remove</button>
</div>
)
}Complete Cart Example
function Cart() {
const { removeItem, 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">
<img src={item.image} alt={item.name} />
<div>
<h4>{item.name}</h4>
<p>Quantity: {item.quantity}</p>
<p>{item.formattedValue}</p>
</div>
<button onClick={() => removeItem(id)}>Remove</button>
</div>
))}
</div>
)
}Notes
- Removes the entire entry, regardless of quantity
- Useful for "Remove from cart" buttons
- For decreasing quantity by 1, use
decrementItem()instead
Interactive Demo
Cart Display
Total Items: 0
Cart is empty. Add items to see them here!
Bananas
Yummy yellow fruit
$4.00
Try it out:
Add items to cart, then remove them completely
Try removing items above! The cart state persists across all documentation pages.
