xxxxxxxxxx
const urlParams = new URLSearchParams(window.location.search);
const page = urlParams.get('page');
xxxxxxxxxx
const params = new URLSearchParams();
params.append('customerId', `${customerId}`);
return await fetchCustomers(`customers/${params}`)
xxxxxxxxxx
new URLSearchParams(location.search).get("your_query_param_key")
______________________________________
if(location && location?.search?.includes("registration_token")){
const emailValue = new URLSearchParams(location.search).get("email")
const regTokenValue = new URLSearchParams(location.search).get("registration_token")
setValue("email", emailValue)
setValue("registration_token", regTokenValue)
console.log(regTokenValue, emailValue);
}
import { useLocation } from 'react-router-dom';
//url = http://localhost:3000/main/ride?params=1
const location = useLocation();
const [value, setValue] = useState( new URLSearchParams(location.search).get('params'));
console.log(value) // 1
xxxxxxxxxx
new URLSearchParams(this.props.location.search).get("your_query_param_key")
xxxxxxxxxx
// Get query parameters in form of object in react
// it automatically detects the url, however one can also provide it
const queryParams = (url = null) => {
const paramsObj = new URLSearchParams(url ? url : window.location.search);
let newObj = {};
for (const [key, value] of paramsObj) {
newObj[key] = value;
}
return newObj;
};
export default queryParams;
// Usage in a component
// Url: http://localhost:3000/properties?query=false&search=true
const params = queryParams()
console.log(params) // Object { query: "false", search: "true" }
xxxxxxxxxx
const getQueryParams = () => window.location.search.replace('?', '').split('&').reduce((r,e) => (r[e.split('=')[0]] = decodeURIComponent(e.split('=')[1]), r), {});