Skip to main content

useCart

Provides access to the cart object.

useCart hook must be a descendent of a CartProvider component.

Examples
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>
);
}


Was this page helpful?