Categories
JavaScript Answers

How to get query parameters’ hash fragment with React-Router v4?

Spread the love

Sometimes, we want to get query parameters’ hash fragment with React-Router v4.

In this article, we’ll look at how to get query parameters’ hash fragment with React-Router v4.

How to get query parameters’ hash fragment with React-Router v4?

To get query parameters’ hash fragment with React-Router v4, we can use the query-string library.

To install it, we run

npm install -save query-string

Then we use it by writing

const queryString = require("query-string");
const parsed = queryString.parse(this.props.location.search);
console.log(parsed.param);

in our component.

We call queryString.parse with the query string we get from this.props.location.search.

And then we get the parameters as an object with parsed.param.

Conclusion

To get query parameters’ hash fragment with React-Router v4, we can use the query-string library.

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 *