Product Providercomponent
is a context provider that enables use of the
hook. It helps manage selected options and variants for a product.
Anchor to propsProps
- Anchor to datadataPartialDeep<Product, {recurseIntoArrays: true}>required
A Storefront API Product object.
- Anchor to childrenchildrenReact.ReactNoderequired
A
element.
- Anchor to initialVariantIdinitialVariantIdInitialVariantId
The initially selected variant. The following logic applies to
: 1. If
is provided, then it's used even if it's out of stock. 2. If
is provided but is
null
, then no variant is used. 3. If nothing is passed tothen the first available / in-stock variant is used. 4. If nothing is passed to
and no variants are in stock, then the first variant is used.
ProductProviderProps
- data
A Storefront API [Product object](https://shopify.dev/api/storefront/reference/products/product).
PartialDeep<Product, {recurseIntoArrays: true}>
- children
A `ReactNode` element.
React.ReactNode
- initialVariantId
The initially selected variant. The following logic applies to `initialVariantId`: 1. If `initialVariantId` is provided, then it's used even if it's out of stock. 2. If `initialVariantId` is provided but is `null`, then no variant is used. 3. If nothing is passed to `initialVariantId` then the first available / in-stock variant is used. 4. If nothing is passed to `initialVariantId` and no variants are in stock, then the first variant is used.
InitialVariantId
interface ProductProviderProps {
/** A Storefront API [Product object](https://shopify.dev/api/storefront/reference/products/product). */
data: PartialDeep<Product, {recurseIntoArrays: true}>;
/** A `ReactNode` element. */
children: React.ReactNode;
/**
* The initially selected variant.
* The following logic applies to `initialVariantId`:
* 1. If `initialVariantId` is provided, then it's used even if it's out of stock.
* 2. If `initialVariantId` is provided but is `null`, then no variant is used.
* 3. If nothing is passed to `initialVariantId` then the first available / in-stock variant is used.
* 4. If nothing is passed to `initialVariantId` and no variants are in stock, then the first variant is used.
*/
initialVariantId?: InitialVariantId;
}
InitialVariantId
ProductVariantType['id'] | null
ProductProvider example
examples
ProductProvider example
description
I am the default example
JavaScript
import {ProductProvider, useProduct} from '@shopify/hydrogen-react'; export function Product({product}) { return ( <ProductProvider data={product} initialVariantId="some-id"> <UsingProduct /> </ProductProvider> ); } function UsingProduct() { const {product, variants, setSelectedVariant} = useProduct(); return ( <> <h1>{product?.title}</h1> {variants?.map((variant) => { <button onClick={() => setSelectedVariant(variant)} key={variant?.id}> {variant?.title} </button>; })} ; </> ); }
TypeScript
import {ProductProvider, useProduct} from '@shopify/hydrogen-react'; import type {Product} from '@shopify/hydrogen-react/storefront-api-types'; export function Product({product}: {product: Product}) { return ( <ProductProvider data={product} initialVariantId="some-id"> <UsingProduct /> </ProductProvider> ); } function UsingProduct() { const {product, variants, setSelectedVariant} = useProduct(); return ( <> <h1>{product?.title}</h1> {variants?.map((variant) => { <button onClick={() => setSelectedVariant(variant)} key={variant?.id}> {variant?.title} </button>; })} ; </> ); }