How to parse query string in react-router v4

前端 未结 7 956
孤独总比滥情好
孤独总比滥情好 2020-12-04 16:27

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

相关标签:
7条回答
  • 2020-12-04 17:00

    Glad I found this post. Thanks for the links, after a couple of hours I finally got my code upgraded.

    For those of you using query-string, you might have to do something like

    var nameYouWant = queryString.parse(this.props.location.search).nameYouWant;
    

    This happened in my case, and this.props.location.search.theUrlYouWant would refuse to work. The second option Tyler mentioned also worked for me with some similar tweaking.

    0 讨论(0)
  • 2020-12-04 17:01

    I'm surprised no one has mentioned UrlSearchParam and the .get method.

    0 讨论(0)
  • 2020-12-04 17:02

    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

    0 讨论(0)
  • 2020-12-04 17:11

    instead of installing a package you can use a simple function for extracting your query params.

    //Param Extractor
    const parseParams = (params = "") => {
      const rawParams = params.replace("?", "").split("&");
      const extractedParams = {};
      rawParams.forEach((item) => {
        item = item.split("=");
        extractedParams[item[0]] = item[1];
      });
      return extractedParams;
    };
    
    //Usage
    const params = parseParams(this.props?.location?.search); // returns an object like:
    // {id:1,name:john...}
    
    
    0 讨论(0)
  • 2020-12-04 17:12

    Using third party package is overkill to simple solutions

    componentDidMount() {
            const query = new URLSearchParams(
                this.props.location.search
            );
    
            let data= {};
    
            for (let params of query.entries()) {
                data[params[0]] = +params[1];
            }
            this.setState({ urldata: data});
        }
    

    this will simply convert URL data into object.

    0 讨论(0)
  • 2020-12-04 17:16

    I proffer my little ES6 shape function, awesome, light weight and useful:

    getQueryStringParams = query => {
        return query
            ? (/^[?#]/.test(query) ? query.slice(1) : query)
                .split('&')
                .reduce((params, param) => {
                        let [key, value] = param.split('=');
                        params[key] = value ? decodeURIComponent(value.replace(/\+/g, ' ')) : '';
                        return params;
                    }, {}
                )
            : {}
    };
    

    Every thing is here, hope to help you.

    0 讨论(0)
提交回复
热议问题