Querystring in react-router

后端 未结 2 1785
小蘑菇
小蘑菇 2021-02-07 00:39

I am trying to set a Route path with a query string. Something in the lines of:

www.mywebsite.com/results?query1=:query1&query2=:query2&query3=:query3
         


        
2条回答
  •  不思量自难忘°
    2021-02-07 01:22

    If you're using React Router >= v4 location.query is not available anymore. You can plug-in an external library (like https://www.npmjs.com/package/query-string), or use something like this:

    const search = props.location.search; // could be '?foo=bar'
    const params = new URLSearchParams(search);
    const foo = params.get('foo'); // bar
    

    (just keep in mind that URLSearchParams() is not supported by Internet Explorer)

提交回复
热议问题