Getting query parameters from react-router hash fragment

后端 未结 9 1611
心在旅途
心在旅途 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:11

    update 2017.12.25

    "react-router-dom": "^4.2.2"

    url like

    BrowserHistory: http://localhost:3000/demo-7/detail/2?sort=name

    HashHistory: http://localhost:3000/demo-7/#/detail/2?sort=name

    with query-string dependency:

    this.id = props.match.params.id;
    this.searchObj = queryString.parse(props.location.search);
    this.from = props.location.state.from;
    
    console.log(this.id, this.searchObj, this.from);
    

    results:

    2 {sort: "name"} home


    "react-router": "^2.4.1"

    Url like http://localhost:8080/react-router01/1?name=novaline&age=26

    const queryParams = this.props.location.query;

    queryParams is a object contains the query params: {name: novaline, age: 26}

提交回复
热议问题