@shopify/hydrogen-react
Version:
React components, hooks, and utilities for creating custom Shopify storefronts
31 lines (30 loc) • 1.07 kB
JavaScript
import { useEffect } from "react";
import { mapSelectedProductOptionToObject } from "./getProductOptions.mjs";
function useSelectedOptionInUrlParam(selectedOptions) {
useEffect(() => {
const optionsSearchParams = new URLSearchParams(
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;
}
export {
useSelectedOptionInUrlParam
};
//# sourceMappingURL=useSelectedOptionInUrlParam.mjs.map