hydrogen
hydrogen copied to clipboard
ProductProvider provides incorrect selectedVariant when data prop is changed
What is the location of your example repository?
No response
Which package or tool is having this issue?
hydrogen-react
What version of that package or tool are you using?
^2024.1.1
What version of Remix are you using?
No response
Steps to Reproduce
Update data and initialVariantId to a different product and variant.
export default function Product({ data, initialVariantId }) {
return (
<ProductProvider data={data} initialVariantId={initialVariantId}>
<ProductTest />
</ProductProvider>
);
}
function ProductTest() {
const { product, selectedVariant } = useProduct();
console.log(product.title, selectedVariant.title);
return (
<div>
{product.title} {selectedVariant.title}
</div>
);
}
Expected Behavior
Using an apparel store as example I would expect the following behaviour:
- Render with "t-shirt" product
dataand black / XLinitialVariantId -
console.logoutputsT-Shirt Black / XL - Change
dataprop to "sweatshirt" product data and red / XSinitialVariantId -
console.logoutputsSweatshirt Red / XS
Actual Behavior
The actual behaviour carries over the selectedOptions to the new product data like this:
- Render with "t-shirt" product
dataand black / XLinitialVariantId -
console.logoutputsT-Shirt Black / XL - Change
dataprop to "sweatshirt" product data and red / XSinitialVariantId -
console.logoutputsSweatshirt Black / XL(provided the product has options with the same name and value available) -
console.logoutputsSweatshirt Red / XS
I think this will depends on how you are changing the data and initialVariantId
Can you provide more information about that?
Closing due to lack of response. Please re-open if still an issue.