Getting query parameters from react-router hash fragment

后端 未结 9 1614
心在旅途
心在旅途 2020-12-04 11:55

I\'m using react and react-router for my application on the client side. I can\'t seem to figure out how to get the following query parameters from a url like:



        
9条回答
  •  无人及你
    2020-12-04 12:28

    Simple js solution:

    queryStringParse = function(string) {
        let parsed = {}
        if(string != '') {
            string = string.substring(string.indexOf('?')+1)
            let p1 = string.split('&')
            p1.map(function(value) {
                let params = value.split('=')
                parsed[params[0]] = params[1]
            });
        }
        return parsed
    }
    

    And you can call it from anywhere using:

    var params = this.queryStringParse(this.props.location.search);
    

    Hope this helps.

提交回复
热议问题