useCartLine
The useCartLine
hook provides access to the cart line object. It must be a descendent of a CartProvider
component.
Example code
import {
CartLinePrice,
CartLineProvider,
useCartLine,
useCart,
} from '@shopify/hydrogen';
export function MyComponent() {
const {lines} = useCart();
return lines.map((line) => {
return (
<CartLineProvider>
<CartLineItem />
</CartLineProvider>
);
});
}
export function CartLineItem() {
const {
quantity,
merchandise: {
product: { title },
},
} = useCartLine();
return (
<>
<h2>{title}</h2>
<span>{quantity}</span>
<CartLinePrice as="span" />
</>
);
}
Return value
The useCartLine
hook returns an object with the following keys:
Name | Description |
---|---|
id | The cart line's ID. |
quantity | The cart line's quantity. |
attributes | The cart line's attributes. |
merchandise | The cart line's associated merchandise. |