Getting query parameters from react-router hash fragment

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

    Note: Copy / Pasted from comment. Be sure to like the original post!

    Writing in es6 and using react 0.14.6 / react-router 2.0.0-rc5. I use this command to lookup the query params in my components:

    this.props.location.query
    

    It creates a hash of all available query params in the url.

    Update:

    For React-Router v4, see this answer. Basically, use this.props.location.search to get the query string and parse with the query-string package or URLSearchParams:

    const params = new URLSearchParams(paramsString); 
    const tags = params.get('tags');
    
    0 讨论(0)
  • 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}

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

    OLD (pre v4):

    Writing in es6 and using react 0.14.6 / react-router 2.0.0-rc5. I use this command to lookup the query params in my components:

    this.props.location.query
    

    It creates a hash of all available query params in the url.

    UPDATE (React Router v4+):

    this.props.location.query in React Router 4 has been removed (currently using v4.1.1) more about the issue here: https://github.com/ReactTraining/react-router/issues/4410

    Looks like they want you to use your own method to parse the query params, currently using this library to fill the gap: https://github.com/sindresorhus/query-string

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

    With stringquery Package:

    import qs from "stringquery";
    
    const obj = qs("?status=APPROVED&page=1limit=20");  
    // > { limit: "10", page:"1", status:"APPROVED" }
    

    With query-string Package:

    import qs from "query-string";
    const obj = qs.parse(this.props.location.search);
    console.log(obj.param); // { limit: "10", page:"1", status:"APPROVED" } 
    

    No Package:

    const convertToObject = (url) => {
      const arr = url.slice(1).split(/&|=/); // remove the "?", "&" and "="
      let params = {};
    
      for(let i = 0; i < arr.length; i += 2){
        const key = arr[i], value = arr[i + 1];
        params[key] = value ; // build the object = { limit: "10", page:"1", status:"APPROVED" }
      }
      return params;
    };
    
    
    const uri = this.props.location.search; // "?status=APPROVED&page=1&limit=20"
    
    const obj = convertToObject(uri);
    
    console.log(obj); // { limit: "10", page:"1", status:"APPROVED" }
    
    
    // obj.status
    // obj.page
    // obj.limit
    

    Hope that helps :)

    Happy coding!

    0 讨论(0)
  • 2020-12-04 12:20
    "react-router-dom": "^5.0.0",
    

    you do not need to add any additional module just in your component that has a url address like this:

    http://localhost:3000/#/?authority'

    you can try the following simple code:

        const search =this.props.location.search;
        const params = new URLSearchParams(search);
        const authority = params.get('authority'); //
    
    0 讨论(0)
  • 2020-12-04 12:23

    You may get the following error while creating an optimized production build when using query-string module.

    Failed to minify the code from this file: ./node_modules/query-string/index.js:8

    To overcome this, kindly use the alternative module called stringquery which does the same process well without any issues while running the build.

    import querySearch from "stringquery";
    
    var query = querySearch(this.props.location.search);
    
    0 讨论(0)
提交回复
热议问题