Skip to main content

useProduct
hook

Provides access to the product value passed to <ProductProvider />. It also includes properties for selecting product variants, options, and selling plans.

PartialDeep< UseProductObjects, {recurseIntoArrays: true} > & UseProductFunctions
(name: string, value: string) => boolean
required

A callback that returns a boolean indicating if the option is in stock.

(name: string, value: string) => void
required

A callback to set the selected option.

(options: ) => void
required

A callback to set multiple selected options at once.

Anchor to setSelectedSellingPlan
setSelectedSellingPlan
(sellingPlan: PartialObjectDeep<SellingPlan, { recurseIntoArrays: true; }>) => void
required

A callback to set the selected selling plan to the one passed as an argument.

(variant: PartialObjectDeep<ProductVariant, { recurseIntoArrays: true; }>) => void
required

A callback to set the selected variant to the variant passed as an argument.

[]
required

An array of the product's options and values.

Product
required

The raw product from the Storefront API

required
ProductVariant[]
required

An array of the variant nodes from the VariantConnection.

SellingPlanType

The selected selling plan.

Anchor to selectedSellingPlanAllocation
selectedSellingPlanAllocation
SellingPlanAllocationType

The selected selling plan allocation.

ProductVariantType | null

The selected variant.

(Omit<SellingPlanGroup, "sellingPlans"> & { sellingPlans: SellingPlan[]; })[]

The selling plan groups.

Anchor to sellingPlanGroupsConnection
sellingPlanGroupsConnection
SellingPlanGroupConnection
ProductVariantConnection
Was this section helpful?

Example code

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