Cart Providercomponent
The component synchronizes the state of the Storefront API Cart and a customer's cart, and allows you to more easily manipulate the cart by adding, removing, and updating it. It could be placed at the root of your app so that your whole app is able to use the
hook anywhere.
There are props that trigger when a call to the Storefront API is made, such as when a line is added to the cart. There are also props that trigger when a call to the Storefront API is completed, such as
when the fetch request for adding a line to the cart completes.
The component must be a descendant of the
component
.
Anchor to propsProps
- Anchor to childrenchildrenReact.ReactNoderequired
Any
elements.
- Anchor to cartFragmentcartFragmentstring
A fragment used to query the Storefront API's Cart object for all queries and mutations. A default value is used if no argument is provided.
- Anchor to countryCodecountryCodeCountryCode
The ISO country code for i18n.
- Anchor to customerAccessTokencustomerAccessTokenstring
A customer access token that's accessible on the server if there's a customer login.
- Anchor to datadataPartialDeep<CartType, {recurseIntoArrays: true}>
An object with fields that correspond to the Storefront API's Cart object.
- Anchor to languageCodelanguageCodeLanguageCode
The ISO language code for i18n.
- Anchor to numCartLinesnumCartLinesnumber
Maximum number of cart lines to fetch. Defaults to 250 cart lines.
- Anchor to onAttributesUpdateonAttributesUpdate() => void
A callback that is invoked when the process to update the cart attributes begins, but before the attributes are updated in the Storefront API.
- Anchor to onAttributesUpdateCompleteonAttributesUpdateComplete() => void
A callback that is invoked when the process to update the cart attributes completes
- Anchor to onBuyerIdentityUpdateonBuyerIdentityUpdate() => void
A callback that is invoked when the process to update the buyer identity begins, but before the buyer identity is updated in the Storefront API.
- Anchor to onBuyerIdentityUpdateCompleteonBuyerIdentityUpdateComplete() => void
A callback that is invoked when the process to update the buyer identity completes
- Anchor to onCreateonCreate() => void
A callback that is invoked when the process to create a cart begins, but before the cart is created in the Storefront API.
- Anchor to onCreateCompleteonCreateComplete() => void
A callback that is invoked when the process to create a cart completes
- Anchor to onDiscountCodesUpdateonDiscountCodesUpdate() => void
A callback that is invoked when the process to update the cart discount codes begins, but before the discount codes are updated in the Storefront API.
- Anchor to onDiscountCodesUpdateCompleteonDiscountCodesUpdateComplete() => void
A callback that is invoked when the process to update the cart discount codes completes
- Anchor to onLineAddonLineAdd() => void
A callback that is invoked when the process to add a line item to the cart begins, but before the line item is added to the Storefront API.
- Anchor to onLineAddCompleteonLineAddComplete() => void
A callback that is invoked when the process to add a line item to the cart completes
- Anchor to onLineRemoveonLineRemove() => void
A callback that is invoked when the process to remove a line item to the cart begins, but before the line item is removed from the Storefront API.
- Anchor to onLineRemoveCompleteonLineRemoveComplete() => void
A callback that is invoked when the process to remove a line item to the cart completes
- Anchor to onLineUpdateonLineUpdate() => void
A callback that is invoked when the process to update a line item in the cart begins, but before the line item is updated in the Storefront API.
- Anchor to onLineUpdateCompleteonLineUpdateComplete() => void
A callback that is invoked when the process to update a line item in the cart completes
- Anchor to onNoteUpdateonNoteUpdate() => void
A callback that is invoked when the process to add or update a note in the cart begins, but before the note is added or updated in the Storefront API.
- Anchor to onNoteUpdateCompleteonNoteUpdateComplete() => void
A callback that is invoked when the process to add or update a note in the cart completes
CartProviderProps
- cartFragment
A fragment used to query the Storefront API's [Cart object](https://shopify.dev/api/storefront/2025-01/objects/cart) for all queries and mutations. A default value is used if no argument is provided.
string
- children
Any `ReactNode` elements.
React.ReactNode
- countryCode
The ISO country code for i18n.
CountryCode
- customerAccessToken
A customer access token that's accessible on the server if there's a customer login.
string
- data
An object with fields that correspond to the Storefront API's [Cart object](https://shopify.dev/api/storefront/2025-01/objects/cart).
PartialDeep<CartType, {recurseIntoArrays: true}>
- languageCode
The ISO language code for i18n.
LanguageCode
- numCartLines
Maximum number of cart lines to fetch. Defaults to 250 cart lines.
number
- onAttributesUpdate
A callback that is invoked when the process to update the cart attributes begins, but before the attributes are updated in the Storefront API.
() => void
- onAttributesUpdateComplete
A callback that is invoked when the process to update the cart attributes completes
() => void
- onBuyerIdentityUpdate
A callback that is invoked when the process to update the buyer identity begins, but before the buyer identity is updated in the Storefront API.
() => void
- onBuyerIdentityUpdateComplete
A callback that is invoked when the process to update the buyer identity completes
() => void
- onCreate
A callback that is invoked when the process to create a cart begins, but before the cart is created in the Storefront API.
() => void
- onCreateComplete
A callback that is invoked when the process to create a cart completes
() => void
- onDiscountCodesUpdate
A callback that is invoked when the process to update the cart discount codes begins, but before the discount codes are updated in the Storefront API.
() => void
- onDiscountCodesUpdateComplete
A callback that is invoked when the process to update the cart discount codes completes
() => void
- onLineAdd
A callback that is invoked when the process to add a line item to the cart begins, but before the line item is added to the Storefront API.
() => void
- onLineAddComplete
A callback that is invoked when the process to add a line item to the cart completes
() => void
- onLineRemove
A callback that is invoked when the process to remove a line item to the cart begins, but before the line item is removed from the Storefront API.
() => void
- onLineRemoveComplete
A callback that is invoked when the process to remove a line item to the cart completes
() => void
- onLineUpdate
A callback that is invoked when the process to update a line item in the cart begins, but before the line item is updated in the Storefront API.
() => void
- onLineUpdateComplete
A callback that is invoked when the process to update a line item in the cart completes
() => void
- onNoteUpdate
A callback that is invoked when the process to add or update a note in the cart begins, but before the note is added or updated in the Storefront API.
() => void
- onNoteUpdateComplete
A callback that is invoked when the process to add or update a note in the cart completes
() => void
{
/** Any `ReactNode` elements. */
children: React.ReactNode;
/** Maximum number of cart lines to fetch. Defaults to 250 cart lines. */
numCartLines?: number;
/** A callback that is invoked when the process to create a cart begins, but before the cart is created in the Storefront API. */
onCreate?: () => void;
/** A callback that is invoked when the process to add a line item to the cart begins, but before the line item is added to the Storefront API. */
onLineAdd?: () => void;
/** A callback that is invoked when the process to remove a line item to the cart begins, but before the line item is removed from the Storefront API. */
onLineRemove?: () => void;
/** A callback that is invoked when the process to update a line item in the cart begins, but before the line item is updated in the Storefront API. */
onLineUpdate?: () => void;
/** A callback that is invoked when the process to add or update a note in the cart begins, but before the note is added or updated in the Storefront API. */
onNoteUpdate?: () => void;
/** A callback that is invoked when the process to update the buyer identity begins, but before the buyer identity is updated in the Storefront API. */
onBuyerIdentityUpdate?: () => void;
/** A callback that is invoked when the process to update the cart attributes begins, but before the attributes are updated in the Storefront API. */
onAttributesUpdate?: () => void;
/** A callback that is invoked when the process to update the cart discount codes begins, but before the discount codes are updated in the Storefront API. */
onDiscountCodesUpdate?: () => void;
/** A callback that is invoked when the process to create a cart completes */
onCreateComplete?: () => void;
/** A callback that is invoked when the process to add a line item to the cart completes */
onLineAddComplete?: () => void;
/** A callback that is invoked when the process to remove a line item to the cart completes */
onLineRemoveComplete?: () => void;
/** A callback that is invoked when the process to update a line item in the cart completes */
onLineUpdateComplete?: () => void;
/** A callback that is invoked when the process to add or update a note in the cart completes */
onNoteUpdateComplete?: () => void;
/** A callback that is invoked when the process to update the buyer identity completes */
onBuyerIdentityUpdateComplete?: () => void;
/** A callback that is invoked when the process to update the cart attributes completes */
onAttributesUpdateComplete?: () => void;
/** A callback that is invoked when the process to update the cart discount codes completes */
onDiscountCodesUpdateComplete?: () => void;
/** An object with fields that correspond to the Storefront API's [Cart object](https://shopify.dev/api/storefront/2025-01/objects/cart). */
data?: PartialDeep<CartType, {recurseIntoArrays: true}>;
/** A fragment used to query the Storefront API's [Cart object](https://shopify.dev/api/storefront/2025-01/objects/cart) for all queries and mutations. A default value is used if no argument is provided. */
cartFragment?: string;
/** A customer access token that's accessible on the server if there's a customer login. */
customerAccessToken?: CartBuyerIdentityInput['customerAccessToken'];
/** The ISO country code for i18n. */
countryCode?: CountryCode;
/** The ISO language code for i18n. */
languageCode?: LanguageCode;
}
Example code
examples
Example code
description
I am the default example
JavaScript
import {CartProvider, useCart} from '@shopify/hydrogen-react'; export function App() { <CartProvider onLineAdd={() => { console.log('a line is being added'); }} onLineAddComplete={() => { console.log('a line has been added'); }} > <CartComponent /> </CartProvider>; } function CartComponent() { const {linesAdd, status} = useCart(); const merchandise = {merchandiseId: '{id-here}'}; return ( <div> Cart Status: {status} <button onClick={() => linesAdd([merchandise])}>Add Line</button> </div> ); }
TypeScript
import {CartProvider, useCart} from '@shopify/hydrogen-react'; import type {CartLineInput} from '@shopify/hydrogen-react/storefront-api-types'; export function App() { <CartProvider onLineAdd={() => { console.log('a line is being added'); }} onLineAddComplete={() => { console.log('a line has been added'); }} > <CartComponent /> </CartProvider>; } function CartComponent() { const {linesAdd, status} = useCart(); const merchandise: CartLineInput = {merchandiseId: '{id-here}'}; return ( <div> Cart Status: {status} <button onClick={() => linesAdd([merchandise])}>Add Line</button> </div> ); }