--- title: useCart description: Provides access to the cart object. api_version: 2023-01 api_name: hydrogen-react source_url: html: 'https://shopify.dev/docs/api/hydrogen-react/2023-01/hooks/usecart' md: 'https://shopify.dev/docs/api/hydrogen-react/2023-01/hooks/usecart.md' --- # use​Cart Provides access to the cart object. ## use​Cart() `useCart` hook must be a descendent of a `CartProvider` component. ### Returns * CartWithActions ### CartWithActions * status "uninitialized" | "fetching" | "creating" | "updating" | "idle" The status of the cart. This returns 'uninitialized' when the cart is not yet created, `creating` when the cart is being created, `fetching` when an existing cart is being fetched, `updating` when the cart is updating, and `idle` when the cart isn't being created or updated. * cartCreate (cart: CartInput) => void A callback that creates a cart. Expects the same input you would provide to the Storefront API's `cartCreate` mutation. * linesAdd (lines: CartLineInput\[]) => void A callback that adds lines to the cart. Expects the same `lines` input that you would provide to the Storefront API's `cartLinesAdd` mutation. If a cart doesn't already exist, then it will create the cart for you. * linesRemove (lines: string\[]) => void A callback that removes lines from the cart. Expects the same `lines` input that you would provide to the Storefront API's `cartLinesRemove` mutation. Only lines that are included in the `lines` parameter will be in the cart afterwards. * linesUpdate (lines: CartLineUpdateInput\[]) => void A callback that updates lines in the cart. Expects the same `lines` input that you would provide to the Storefront API's `cartLinesUpdate` mutation. If a line item is not included in the `lines` parameter, it will still exist in the cart and will not be changed. * noteUpdate (note: string) => void A callback that updates the note in the cart. Expects the same `note` input that you would provide to the Storefront API's `cartNoteUpdate` mutation. * buyerIdentityUpdate (buyerIdenity: CartBuyerIdentityInput) => void A callback that updates the buyer identity in the cart. Expects the same `buyerIdentity` input that you would provide to the Storefront API's `cartBuyerIdentityUpdate` mutation. * cartAttributesUpdate (attributes: AttributeInput\[]) => void A callback that updates the cart attributes. Expects the same `attributes` input that you would provide to the Storefront API's `cartAttributesUpdate` mutation. * discountCodesUpdate (discountCodes: string\[]) => void A callback that updates the cart's discount codes. Expects the same `codes` input that you would provide to the Storefront API's `cartDiscountCodesUpdate` mutation. * totalQuantity number The total number of items in the cart, across all lines. If there are no lines, then the value is 0. * cartFragment string The fragment used to query the cart object for all queries and mutations. * error unknown If an error occurred on the previous cart action, then `error` will exist and `cart` will be put back into the last valid status it was in. ### CartWithActions * status The status of the cart. This returns 'uninitialized' when the cart is not yet created, \`creating\` when the cart is being created, \`fetching\` when an existing cart is being fetched, \`updating\` when the cart is updating, and \`idle\` when the cart isn't being created or updated. ```ts "uninitialized" | "fetching" | "creating" | "updating" | "idle" ``` * error If an error occurred on the previous cart action, then \`error\` will exist and \`cart\` will be put back into the last valid status it was in. ```ts unknown ``` * cartCreate A callback that creates a cart. Expects the same input you would provide to the Storefront API's \`cartCreate\` mutation. ```ts (cart: CartInput) => void ``` * linesAdd A callback that adds lines to the cart. Expects the same \`lines\` input that you would provide to the Storefront API's \`cartLinesAdd\` mutation. If a cart doesn't already exist, then it will create the cart for you. ```ts (lines: CartLineInput[]) => void ``` * linesRemove A callback that removes lines from the cart. Expects the same \`lines\` input that you would provide to the Storefront API's \`cartLinesRemove\` mutation. Only lines that are included in the \`lines\` parameter will be in the cart afterwards. ```ts (lines: string[]) => void ``` * linesUpdate A callback that updates lines in the cart. Expects the same \`lines\` input that you would provide to the Storefront API's \`cartLinesUpdate\` mutation. If a line item is not included in the \`lines\` parameter, it will still exist in the cart and will not be changed. ```ts (lines: CartLineUpdateInput[]) => void ``` * noteUpdate A callback that updates the note in the cart. Expects the same \`note\` input that you would provide to the Storefront API's \`cartNoteUpdate\` mutation. ```ts (note: string) => void ``` * buyerIdentityUpdate A callback that updates the buyer identity in the cart. Expects the same \`buyerIdentity\` input that you would provide to the Storefront API's \`cartBuyerIdentityUpdate\` mutation. ```ts (buyerIdenity: CartBuyerIdentityInput) => void ``` * cartAttributesUpdate A callback that updates the cart attributes. Expects the same \`attributes\` input that you would provide to the Storefront API's \`cartAttributesUpdate\` mutation. ```ts (attributes: AttributeInput[]) => void ``` * discountCodesUpdate A callback that updates the cart's discount codes. Expects the same \`codes\` input that you would provide to the Storefront API's \`cartDiscountCodesUpdate\` mutation. ```ts (discountCodes: string[]) => void ``` * totalQuantity The total number of items in the cart, across all lines. If there are no lines, then the value is 0. ```ts number ``` * cartFragment The fragment used to query the cart object for all queries and mutations. ```ts string ``` ```ts export interface CartWithActions extends Cart { /** The status of the cart. This returns 'uninitialized' when the cart is not yet created, `creating` when the cart is being created, `fetching` when an existing cart is being fetched, `updating` when the cart is updating, and `idle` when the cart isn't being created or updated. */ status: CartStatus; /** If an error occurred on the previous cart action, then `error` will exist and `cart` will be put back into the last valid status it was in. */ error?: unknown; /** A callback that creates a cart. Expects the same input you would provide to the Storefront API's `cartCreate` mutation. */ cartCreate: (cart: CartInput) => void; /** A callback that adds lines to the cart. Expects the same `lines` input that you would provide to the Storefront API's `cartLinesAdd` mutation. If a cart doesn't already exist, then it will create the cart for you. */ linesAdd: (lines: CartLineInput[]) => void; /** A callback that removes lines from the cart. Expects the same `lines` input that you would provide to the Storefront API's `cartLinesRemove` mutation. Only lines that are included in the `lines` parameter will be in the cart afterwards. */ linesRemove: (lines: string[]) => void; /** A callback that updates lines in the cart. Expects the same `lines` input that you would provide to the Storefront API's `cartLinesUpdate` mutation. If a line item is not included in the `lines` parameter, it will still exist in the cart and will not be changed. */ linesUpdate: (lines: CartLineUpdateInput[]) => void; /** A callback that updates the note in the cart. Expects the same `note` input that you would provide to the Storefront API's `cartNoteUpdate` mutation. */ noteUpdate: (note: MutationCartNoteUpdateArgs['note']) => void; /** A callback that updates the buyer identity in the cart. Expects the same `buyerIdentity` input that you would provide to the Storefront API's `cartBuyerIdentityUpdate` mutation. */ buyerIdentityUpdate: (buyerIdenity: CartBuyerIdentityInput) => void; /** A callback that updates the cart attributes. Expects the same `attributes` input that you would provide to the Storefront API's `cartAttributesUpdate` mutation. */ cartAttributesUpdate: ( attributes: MutationCartAttributesUpdateArgs['attributes'], ) => void; /** A callback that updates the cart's discount codes. Expects the same `codes` input that you would provide to the Storefront API's `cartDiscountCodesUpdate` mutation. */ discountCodesUpdate: (discountCodes: string[]) => void; /** The total number of items in the cart, across all lines. If there are no lines, then the value is 0. */ totalQuantity: number; /** The fragment used to query the cart object for all queries and mutations. */ cartFragment: string; } ``` Examples ### Examples * #### Example code ##### Description I am the default example ##### JavaScript ```jsx import {CartProvider, useCart} from '@shopify/hydrogen-react'; export function App() { { console.log('a line is being added'); }} onLineAddComplete={() => { console.log('a line has been added'); }} > ; } function CartComponent() { const {linesAdd, status} = useCart(); const merchandise = {merchandiseId: '{id-here}'}; return (
Cart Status: {status}
); } ``` ##### TypeScript ```tsx import {CartProvider, useCart} from '@shopify/hydrogen-react'; import type {CartLineInput} from '@shopify/hydrogen-react/storefront-api-types'; export function App() { { console.log('a line is being added'); }} onLineAddComplete={() => { console.log('a line has been added'); }} > ; } function CartComponent() { const {linesAdd, status} = useCart(); const merchandise: CartLineInput = {merchandiseId: '{id-here}'}; return (
Cart Status: {status}
); } ``` ## Related [- CartProvider](https://shopify.dev/api/hydrogen-react/components/CartProvider)