react-use-cart
A lightweight shopping cart hook for React, Next.js, and Gatsby
Why?
- No dependencies
- ? Not tied to any payment gateway, or checkout - create your own!
- ? Persistent carts with local storage, or your own adapter
- ⭐️ Supports multiples carts per page
- ? Flexible cart item schema
- ? Works with Next, Gatsby, React
- ♻️ Trigger your own side effects with cart handlers (on item add, update, remove)
- ? Built with TypeScript
- ✅ Fully tested
- ? Used by Dines
Quick Start
Install
CartProvider
You will need to wrap your application with the CartProvider
component so that the useCart
hook can access the cart state.
Carts are persisted across visits using localStorage
, unless you specify your own storage
adapter.
Usage
Props
Prop | Required | Description |
---|---|---|
id |
No | id for your cart to enable automatic cart retrieval via window.localStorage . If you pass a id then you can use multiple instances of CartProvider . |
onSetItems |
No | Triggered only when setItems invoked. |
onItemAdd |
No | Triggered on items added to your cart, unless the item already exists, then onItemUpdate will be invoked. |
onItemUpdate |
No | Triggered on items updated in your cart, unless you are setting the quantity to 0 , then onItemRemove will be invoked. |
onItemRemove |
No | Triggered on items removed from your cart. |
storage |
No | Must return [getter, setter] . |
metadata |
No | Custom global state on the cart. Stored inside of metadata . |
useCart
The useCart
hook exposes all the getter/setters for your cart state.
setItems(items)
The setItems
method should be used to set all items in the cart. This will overwrite any existing cart items. A quantity
default of 1 will be set for an item implicitly if no quantity
is specified.
Args
items[]
(Required): An array of cart item object. You must provide anid
andprice
value for new items that you add to cart.
Usage
addItem(item, quantity)
The addItem
method should be used to add items to the cart.
Args
item
(Required): An object that represents your cart item. You must provide anid
andprice
value for new items that you add to cart.quantity
(optional, default:1
): The amount of items you want to add.
Usage
updateItem(itemId, data)
The updateItem
method should be used to update items in the cart.
Args
itemId
(Required): The cart itemid
you want to update.data
(Required): The updated cart item object.
Usage
updateItemQuantity(itemId, quantity)
The updateItemQuantity
method should be used to update an items quantity
value.
Args
itemId
(Required): The cart itemid
you want to update.quantity
(Required): The updated cart item quantity.
Usage
removeItem(itemId)
The removeItem
method should be used to remove an item from the cart.
Args
itemId
(Required): The cart itemid
you want to remove.
Usage
emptyCart()
The emptyCart()
method should be used to remove all cart items, and resetting cart totals to the default 0
values.
Usage
clearCartMetadata()
The clearCartMetadata()
will reset the metadata
to an empty object.
Usage
setCartMetadata(object)
The setCartMetadata()
will replace the metadata
object on the cart. You must pass it an object.
Args
object
: A object with key/value pairs. The key being a string.
Usage
updateCartMetadata(object)
The updateCartMetadata()
will update the metadata
object on the cart. You must pass it an object. This will merge the passed object with the existing metadata.
Args
object
: A object with key/value pairs. The key being a string.
Usage
items = []
This will return the current cart items in an array.
Usage
isEmpty = false
A quick and easy way to check if the cart is empty. Returned as a boolean.
Usage
getItem(itemId)
Get a specific cart item by id
. Returns the item object.
Args
itemId
(Required): Theid
of the item you're fetching.
Usage
inCart(itemId)
Quickly check if an item is in the cart. Returned as a boolean.
Args
itemId
(Required): Theid
of the item you're looking for.
Usage
totalItems = 0
This returns the totaly quantity of items in the cart as an integer.
Usage
totalUniqueItems = 0
This returns the total unique items in the cart as an integer.
Usage
cartTotal = 0
This returns the total value of all items in the cart.
Usage
metadata = {}
This returns the metadata set with updateCartMetadata
. This is useful for storing additional cart, or checkout values.