Issue
The Route
's path
prop only specifies route params
, not query string parameters.
For example, for path="/root/:id"
if a user were to navigate to "/root/test"
, then inside a component you could access the route params from the match object. Something like the following:
const { id } = props.match;
console.log(id); // "test"
Even if a user navigated to "/root/test?qp1=hello&qp2=world"
the route params would still only pick up id
from the path.
You also can't assign the same match param to multiple path segments. path="/salesoff/:id/salesdist/:id/custname/:id/product/:id/productgroup/:id"
can't/shouldn't all assign to match.params.id
.
Solution
You can, however, get the entire query string from the location object, and in this case it is the whole query string as a single string. Given the same path from before, "/root/test?qp1=hello&qp2=world"
, then you can access the query string as follows:
const { search } = useLocation();
console.log(search); // "?qp1=hello&qp2=world"
It's up to you to now parse the query string if you want to access specific key-value pairs.
const { search } = useLocation();
const urlParams = Object.fromEntries([...new URLSearchParams(search)]);
console.log(urlParams); // { qp1: 'hello', qp2: 'world' }
Demo
Demo with your code and URL "/?salesoff=S4&salesdist=District%201&custname=C2&product=P2&productgroup=PG-1"
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…