useCart
The useCart
hook gives access to the Cart
object and helper functions, such as addToItem
and removeItem
, for applying changes in the cart.
Import​
import { useCart } from '@faststore/sdk'
Usage​
function Cart () {
const { items, isValidating } = useCart()
return (
<>
{items.map(item => (
<div>name: {item.itemOffered.name}</div>
<div>Price: {item.price}</div>
))}
<a href="/checkout">{isValidating ? 'loading...' : 'Checkout'}</a>
</>
)
}
Return​
addItem
(item: T) => void
Description
Adds items to the cart.
emptyCart
() => void
Description
Removes all items from the cart.
getItem
(id: string) => T | undefined
Description
Gets a given item from the cart.
id
string
Description
Gets the cart's id.
inCart
(id: string) => boolean
Description
Checks if an item is in the cart.
isEmpty
boolean
Description
Checks if the cart is empty.
isValidating
boolean
Description
Returns `true` if the optimistic cart is validating the cart.
items
Item
Description
Gets the current items in the cart.
removeItem
(id: string) => void
Description
Removes an item from the cart.
setCart
(cart: Cart) => void
Description
Replaces the cart with a new one.
updateItemQuantity
(id: string, quantity: number) => void
Description
Updates the quantity of a given item in the cart.