use Product
Provides access to the product value passed to <ProductProvider />. It also includes properties for selecting product variants, options, and selling plans.
Anchor to propsProps
PartialDeep<
UseProductObjects,
{recurseIntoArrays: true}
> & UseProductFunctionsPartialDeep<
UseProductObjects,
{recurseIntoArrays: true}
> & UseProductFunctionsUseProductFunctions
- Anchor to isOptionInStockis
Option In Stockis Option In Stock (name: string, value: string) => boolean(name: string, value: string) => booleanrequiredrequired A callback that returns a boolean indicating if the option is in stock.
- Anchor to setSelectedOptionset
Selected Optionset Selected Option (name: string, value: string) => void(name: string, value: string) => voidrequiredrequired A callback to set the selected option.
- Anchor to setSelectedOptionsset
Selected Optionsset Selected Options (options: SelectedOptions) => void(options: SelectedOptions) => voidrequiredrequired A callback to set multiple selected options at once.
- Anchor to setSelectedSellingPlanset
Selected Selling Planset Selected Selling Plan ( sellingPlan: PartialDeep<SellingPlanType, {recurseIntoArrays: true}>, ) => void( sellingPlan: PartialDeep<SellingPlanType, {recurseIntoArrays: true}>, ) => voidrequiredrequired A callback to set the selected selling plan to the one passed as an argument.
- Anchor to setSelectedVariantset
Selected Variantset Selected Variant ( variant: PartialDeep<ProductVariantType, {recurseIntoArrays: true}> | null, ) => void( variant: PartialDeep<ProductVariantType, {recurseIntoArrays: true}> | null, ) => voidrequiredrequired A callback to set the selected variant to the variant passed as an argument.
UseProductObjects
- Anchor to optionsoptionsoptionsOptionWithValues[]OptionWithValues[]requiredrequired
An array of the product's options and values.
- Anchor to productproductproductProductProductrequiredrequired
The raw product from the Storefront API
- Anchor to selectedOptionsselected
Optionsselected Options SelectedOptionsSelectedOptionsrequiredrequired - Anchor to variantsvariantsvariantsProductVariant[]ProductVariant[]requiredrequired
An array of the variant
nodesfrom the.- Anchor to selectedSellingPlanselected
Selling Planselected Selling Plan SellingPlanSellingPlan The selected selling plan.
- Anchor to selectedSellingPlanAllocationselected
Selling Plan Allocationselected Selling Plan Allocation SellingPlanAllocationSellingPlanAllocation The selected selling plan allocation.
- Anchor to selectedVariantselected
Variantselected Variant ProductVariantType | nullProductVariantType | null The selected variant.
- Anchor to sellingPlanGroupsselling
Plan Groupsselling Plan Groups (Omit<SellingPlanGroup, "sellingPlans"> & { sellingPlans: SellingPlan[]; })[](Omit<SellingPlanGroup, "sellingPlans"> & { sellingPlans: SellingPlan[]; })[] The selling plan groups.
- Anchor to sellingPlanGroupsConnectionselling
Plan Groups Connectionselling Plan Groups Connection SellingPlanGroupConnectionSellingPlanGroupConnection An auto-generated type for paginating through multiple SellingPlanGroups.
- Anchor to variantsConnectionvariants
Connectionvariants Connection ProductVariantConnectionProductVariantConnection An auto-generated type for paginating through multiple ProductVariants.
UseProductObjects
- options
An array of the product's options and values.
OptionWithValues[] - product
The raw product from the Storefront API
Product - selectedOptions
SelectedOptions - selectedSellingPlan
The selected selling plan.
SellingPlan - selectedSellingPlanAllocation
The selected selling plan allocation.
SellingPlanAllocation - selectedVariant
The selected variant.
ProductVariantType | null - sellingPlanGroups
The selling plan groups.
(Omit<SellingPlanGroup, "sellingPlans"> & { sellingPlans: SellingPlan[]; })[] - sellingPlanGroupsConnection
An auto-generated type for paginating through multiple SellingPlanGroups.
SellingPlanGroupConnection - variants
An array of the variant `nodes` from the `VariantConnection`.
ProductVariant[] - variantsConnection
An auto-generated type for paginating through multiple ProductVariants.
ProductVariantConnection
OptionWithValues
- name
string - values
string[]
SelectedOptions
UseProductFunctions
- isOptionInStock
A callback that returns a boolean indicating if the option is in stock.
(name: string, value: string) => boolean - setSelectedOption
A callback to set the selected option.
(name: string, value: string) => void - setSelectedOptions
A callback to set multiple selected options at once.
(options: SelectedOptions) => void - setSelectedSellingPlan
A callback to set the selected selling plan to the one passed as an argument.
( sellingPlan: PartialDeep<SellingPlanType, {recurseIntoArrays: true}>, ) => void - setSelectedVariant
A callback to set the selected variant to the variant passed as an argument.
( variant: PartialDeep<ProductVariantType, {recurseIntoArrays: true}> | null, ) => void
Examples
Example code
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>; })} ; </> ); }