I have this url http://localhost:3000/#/consultation/?idc=xxxd?iduser=Hyfzjg2 and I want to get the value of idc and iduser for using it on my APIs with ReactJs.
I try this:
let isInitialized = false;
const initialValues = {};
const initValues = () => {
if (isInitialized === false) {
const searchParams = new URLSearchParams(window.location.hash);
const values = [
{ urlKey: "idc", stateKey: "idC" },
{ urlKey: "iduser", stateKey: "idUser" },
];
values.forEach(item => {
const urlValue = searchParams.get(item.urlKey)
if (urlValue) {
initialValues[item.stateKey] = urlValue;
}
});
isInitialized = true;
}
return initialValues;
};
console.log(initValues())
When I run it, I get {}
an empty value.
How can I get the value of idc
and iduser
?