Search code examples
reactjsreact-routerreact-router-dom

How to parse query string in react-router v4


In react-router v3 I could access it with props.location.query.foo (if the current location was ?foo=bar)

In react-router-dom@4.0.0 props.location only has props.location.search with is a string like ?foo=bar&other=thing.

Perhaps I need to manually parse and deconstruct that string to find the value for foo or other.

Screenshot of console.log(this.props): enter image description here (Note how from ?artist=band here I'd like to get the value from artist which is the value band)


Solution

  • Looks like you already assumed correct. The ability to parse query strings was taken out of V4 because there have been requests over the years to support different implementation. With that, the team decided it would be best for users to decide what that implementation looks like. We recommend importing a query string lib. The one you mentioned has worked great for me so far.

    const queryString = require('query-string');
    
    const parsed = queryString.parse(props.location.search);
    

    You can also use new URLSearchParams if you want something native and it works for your needs

    const search = props.location.search; // could be '?foo=bar'
    const params = new URLSearchParams(search);
    const foo = params.get('foo'); // bar
    

    You can read more about the decision here