Add To Cart Button
The AddToCartButton component renders a button that adds an item to the cart when pressed.
It must be a descendent of the CartProvider component.
Anchor to propsProps
- Anchor to childrenchildrenchildrenReactNodeReactNoderequiredrequired
Any ReactNode elements.
- Anchor to accessibleAddingToCartLabelaccessible
Adding To Cart Labelaccessible Adding To Cart Label stringstring The text that is announced by the screen reader when the item is being added to the cart. Used for accessibility purposes only and not displayed on the page.
- Anchor to asasasAsTypeAsType
Provide a React element or component to render as the underlying button. Note: for accessibility compliance, almost always you should use a
buttonelement, or a component that renders an underlying button.- Anchor to attributesattributesattributes{ key: string; value: string; }[]{ key: string; value: string; }[]
An array of cart line attributes that belong to the item being added to the cart.
- Ref<HTMLButtonElement>Ref<HTMLButtonElement>
A
refto the underlying button- Anchor to defaultOnClickdefault
On Clickdefault On Click (event?: MouseEvent<HTMLButtonElement, MouseEvent>) => boolean | void(event?: MouseEvent<HTMLButtonElement, MouseEvent>) => boolean | void A default
behavior- Anchor to onClickon
Clickon Click (event?: MouseEvent<HTMLButtonElement, MouseEvent>) => boolean | void(event?: MouseEvent<HTMLButtonElement, MouseEvent>) => boolean | void Click event handler. Default behaviour triggers unless prevented
- Anchor to parentparentparentCartLineParentInputCartLineParentInput
The parent line item of the item being added to the cart. Used for nested cart lines.
- Anchor to quantityquantityquantitynumbernumber
The item quantity.
- Anchor to sellingPlanIdselling
Plan Idselling Plan Id stringstring The selling plan ID of the subscription variant
- Anchor to variantIdvariant
Idvariant Id string | nullstring | null The ID of the variant.
Examples
Example code
Description
I am the default example
JavaScript
import {AddToCartButton} from '@shopify/hydrogen-react'; export default function ProductAddToCartButton({product}) { const variantId = product.variants[0].id; if (!variantId) { return null; } return <AddToCartButton variantId={variantId} />; }TypeScript
import {AddToCartButton} from '@shopify/hydrogen-react'; import type {Product} from '@shopify/hydrogen-react/storefront-api-types'; export default function ProductAddToCartButton({product}: {product: Product}) { const variantId = product.variants[0].id; if (!variantId) { return null; } return <AddToCartButton variantId={variantId} />; }