1// example url: https://mydomain.com/?fname=johnny&lname=depp
2const queryString = window.location.search;
3console.log(queryString);
4// ?fname=johnny&lname=depp
5
6const urlParams = new URLSearchParams(queryString);
7
8const firstName = urlParams.get('fname');
9console.log(firstName);
10// johnny
11
12const lastName = urlParams.get('lname');
13console.log(lastName);
14// depp
1const queryString = window.location.search;
2const parameters = new URLSearchParams(queryString);
3const value = parameters.get('key');
1const urlParams = new URLSearchParams(window.location.search);
2const myParam = urlParams.get('myParam');
3
1const urlParams = new URLSearchParams(window.location.search);
2const myParam = urlParams.get('myParam');
1// example url: https://mydomain.com/?fname=johnny&lname=depp
2const urlParams = new URLSearchParams(window.location.search);
3const firstName = urlParams.get('fname'); // johnny
1import React, { useEffect, useState } from "react";
2import { useLocation } from "react-router-dom";
3
4function CheckoutDetails() {
5 const location = useLocation();
6 const [amountValue, setAmountValue] = useState(1);
7
8 // function to get query params using URLSearchParams
9 useEffect(() => {
10 const searchParams = new URLSearchParams(location.search);
11 if (searchParams.has("amount")) {
12 const amount = searchParams.get("amount");
13 setAmountValue(parseInt(amount, 10));
14 } else {
15 setAmountValue(1);
16 }
17 }, [location]);
18
19 return (
20 <p>Amount: {amountValue}</p>
21 )
22