@shopify/hydrogen-react
Version:
React components, hooks, and utilities for creating custom Shopify storefronts
31 lines (30 loc) • 1.19 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const React = require("react");
const getProductOptions = require("./getProductOptions.js");
function useSelectedOptionInUrlParam(selectedOptions) {
React.useEffect(() => {
const optionsSearchParams = new URLSearchParams(
getProductOptions.mapSelectedProductOptionToObject(selectedOptions || [])
);
const currentSearchParams = new URLSearchParams(window.location.search);
const combinedSearchParams = new URLSearchParams({
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
...Object.fromEntries(currentSearchParams),
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
...Object.fromEntries(optionsSearchParams)
});
if (combinedSearchParams.size > 0) {
window.history.replaceState(
{},
"",
`${window.location.pathname}?${combinedSearchParams.toString()}`
);
}
}, [JSON.stringify(selectedOptions)]);
return null;
}
exports.useSelectedOptionInUrlParam = useSelectedOptionInUrlParam;
//# sourceMappingURL=useSelectedOptionInUrlParam.js.map