Getting query parameters from react-router hash fragment

后端 未结 9 1556
心在旅途
心在旅途 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.

    0 讨论(0)
  • 2020-12-04 12:30

    The above answers won't work in react-router v4. Here's what I did to solve the problem -

    First Install query-string which will be required for parsing.

    npm install -save query-string

    Now in the routed component you can access the un-parsed query string like this

    this.props.location.search

    You can cross check it by logging in the console.

    Finally parse to access the query parameters

    const queryString = require('query-string');
    var parsed = queryString.parse(this.props.location.search);
    console.log(parsed.param); // replace param with your own 
    

    So if query is like ?hello=world

    console.log(parsed.hello) will log world

    0 讨论(0)
  • 2020-12-04 12:30

    After reading the other answers (First by @duncan-finney and then by @Marrs) I set out to find the change log that explains the idiomatic react-router 2.x way of solving this. The documentation on using location (which you need for queries) in components is actually contradicted by the actual code. So if you follow their advice, you get big angry warnings like this:

    Warning: [react-router] `context.location` is deprecated, please use a route component's `props.location` instead.
    

    It turns out that you cannot have a context property called location that uses the location type. But you can use a context property called loc that uses the location type. So the solution is a small modification on their source as follows:

    const RouteComponent = React.createClass({
        childContextTypes: {
            loc: PropTypes.location
        },
    
        getChildContext() {
            return { location: this.props.location }
        }
    });
    
    const ChildComponent = React.createClass({
        contextTypes: {
            loc: PropTypes.location
        },
        render() {
            console.log(this.context.loc);
            return(<div>this.context.loc.query</div>);
        }
    });
    

    You could also pass down only the parts of the location object you want in your children get the same benefit. It didn't change the warning to change to the object type. Hope that helps.

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