An extension UI in with this piece of code using useBuyerJourneyIntercept causes the extension to throw an error - Minified React error #300 .
This happens only in production after the extension is deploys, not throw the dev mode with the CLI.
There is a related issue here at github about this.
opened 06:23AM - 19 Apr 24 UTC
bug
## Please list the package(s) involved in the issue, and include the version you… are using
api_version = "2024-04"
"dependencies": {
"@shopify/app": "3.58.2",
"@shopify/cli": "3.59.0"
},
## Describe the bug
See the video below which throws a react error only when the preview theme is incognito and the postal code is entered. It is related to the `useBuyerJourneyIntercept` because when I emit from the following code I no longer see the error. The full code to my ui extension is listed below:
```js
import {
reactExtension,
useShippingAddress,
useTranslate,
useCartLines,
useBuyerJourneyIntercept
} from '@shopify/ui-extensions-react/checkout';
export default reactExtension(
'purchase.checkout.block.render',
() => <Extension />,
);
/**
* Checks if any line items have the _kyoto_only property and if the shipping address is not based in kyoto to block progress with error messaging
* @returns {null}
*/
function Extension() {
const address = useShippingAddress();
const translate = useTranslate();
const prefecture = address?.provinceCode;
const kyotoPrefecture = 'JP-26';
const lineItems = useCartLines();
const property_match = '_kyoto_only';
if (!lineItems) {
return;
}
// Check if any line item has the _kyoto_only property
const hasKyotoOnlyItem = lineItems?.some(item => {
return item.attributes.some(attribute => attribute?.key === property_match && attribute?.value === 'true');
});
if (!hasKyotoOnlyItem || !prefecture) {
return;
}
// Block progress if the shipping address is not based in kyoto and the cart contains a line item with property_match
useBuyerJourneyIntercept(
({canBlockProgress}) => {
if (!canBlockProgress || !hasKyotoOnlyItem || !prefecture || prefecture === kyotoPrefecture) {
return { behavior: 'allow' };
}
return {
behavior: 'block',
reason: translate("kyoto_only.reason"),
errors: [
{
message: translate("kyoto_only.inline_error_message"),
// Show an error underneath the province code field
target: '$.cart.deliveryGroups[0].deliveryAddress.provinceCode',
},
{
// In addition, show an error at th top of the page
message: translate("kyoto_only.banner_error_message"),
},
],
};
},
);
return null;
}
```
## Steps to reproduce the behavior:
https://github.com/Shopify/ui-extensions/assets/12322743/8c14397d-85ae-47d3-8aeb-bb84feac625d
## Expected behavior
No error is thrown when first entering shipping information
## Screenshots
https://github.com/Shopify/ui-extensions/assets/12322743/8c14397d-85ae-47d3-8aeb-bb84feac625d
The code:
const toBlockDropshipCheckout = !mandatoryProductsIncluded || !dropshippingOnlyCart;
if (toBlockDropshipCheckout) {
useBuyerJourneyIntercept(({ canBlockProgress }) => {
if (canBlockProgress) {
if(!mandatoryProductsIncluded){
return {
behavior: 'block',
reason: 'There is no mandatory dropship product in the cart',
errors: [
{
message: translate('mandatoryProductsIncluded'),
},
],
};
}
if(!dropshippingOnlyCart){
return {
behavior: 'block',
reason: 'The cart contains non-dropshipped products',
errors: [
{
message: translate('dropshippingOnlyCart'),
},
],
};
}
}
return {
behavior: 'allow',
};
});
}
if (toBlockDropshipCheckout) {
return null;
}
return (
<TextField
label='Dropship email'
onChange={(value) => {
applyMetafieldsChange({
type: 'updateMetafield',
valueType: 'string',
namespace: metafieldNamespace,
key: metafieldKey,
value: dropshipEmailMetafield?.value || value,
});
}}
/>
);
Hey Alon, are you getting this error? Minified React error #310 – React
I see in your code snippet you are calling the hook useBuyerJourneyIntercept
conditionally. Can you try moving it out of the conditional, and put it at the component’s top level?
Please see Rules of Hooks – React
2 Likes
Worked! Thanks @avocadomayo