Skip to main content

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.

idstring
Description

Gets the cart's id.

inCart(id: string) => boolean
Description

Checks if an item is in the cart.

isEmptyboolean
Description

Checks if the cart is empty.

isValidatingboolean
Description

Returns `true` if the optimistic cart is validating the cart.

itemsItem
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.

Didn't find your answers? Ask the Community. For documentation suggestions, submit your feedback.

JOIN THE COMMUNITY

ON THIS PAGE