Categories
React Answers

How to get parameter value from query string with React?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *