Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the raw product returned from the Storefront API to also return from useProduct #735

Merged
merged 5 commits into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .changeset/few-yaks-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'@shopify/hydrogen-react': patch
---

Add the raw product returned from the Storefront API to also return from `useProduct()`:

```ts
function SomeComponent() {
const {product} = useProduct();

return (
<div>
<h2>{product.title}</h2>
<h3>{product.description}</h3>
</div>
);
}
```
11 changes: 11 additions & 0 deletions packages/hydrogen-react/src/ProductProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ describe('<ProductProvider />', () => {
]);
});

it('returns full product', () => {
const product = getProduct({variants: VARIANTS});
const {result} = renderHook(() => useProduct(), {
wrapper: ({children}) => (
<ProductProvider data={product}>{children}</ProductProvider>
),
});

expect(result.current.product).toEqual(product);
});

it('provides setSelectedOption callback', async () => {
const user = userEvent.setup();

Expand Down
6 changes: 4 additions & 2 deletions packages/hydrogen-react/src/ProductProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ export function ProductProvider({

const value = useMemo<ProductHookValue>(
() => ({
product,
variants,
variantsConnection: product.variants,
options,
Expand All @@ -183,10 +184,9 @@ export function ProductProvider({
sellingPlanGroupsConnection: product.sellingPlanGroups,
}),
[
product,
isOptionInStock,
options,
product.sellingPlanGroups,
product.variants,
Comment on lines -188 to -189
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm assuming that this won't be a problem, changing the memo dependency array. I'd think if the product changes, so would these arrays. Also if these arrays change, so would the wrapping product.

selectedOptions,
selectedSellingPlan,
selectedSellingPlanAllocation,
Expand Down Expand Up @@ -335,6 +335,8 @@ export interface OptionWithValues {

type ProductHookValue = PartialDeep<
{
/** The raw product from the Storefront API */
product: Product;
/** An array of the variant `nodes` from the `VariantConnection`. */
variants: ProductVariantType[];
variantsConnection?: ProductVariantConnection;
Expand Down