Sometimes, we want to get parameter value from query string with React.
In this article, we’ll look at how to get parameter value from query string with React.
How to get parameter value from query string with React?
To get parameter value from query string with React, we use the URLSearchParams
constructor.
For instance, we write
const search = window.location.search;
const params = new URLSearchParams(search);
const foo = params.get("bar");
to get the query string with window.location.search
.
Then we pass that in as the value of the URLSearchParams
constructor.
And then we get the value of the query parameter with key bar
with params.get("bar")
.
Conclusion
To get parameter value from query string with React, we use the URLSearchParams
constructor.