如何在react-router v4中获取查询参数

and*_*fas 77 react-router react-router-v4 react-router-dom

我在我的项目中使用react-router-dom 4.0.0-beta.6.我有一个如下代码:

<Route exact path="/home" component={HomePage}/>
Run Code Online (Sandbox Code Playgroud)

我想在HomePage组件中获取查询参数.我找到了location.searchparam,看起来像这样:?key=value所以它是未解析的.

使用react-router v4获取查询参数的正确方法是什么?

Tyl*_*nis 153

解析查询字符串的能力是从V4中取出的,因为多年来一直有人要求支持不同的实现.有了这个,团队决定最好让用户决定实现的样子.我们建议导入查询字符串lib.这是我使用的一个

const queryString = require('query-string');

const parsed = queryString.parse(props.location.search);
Run Code Online (Sandbox Code Playgroud)

您也可以使用,new URLSearchParams如果您想要原生的东西,它可以满足您的需求

const params = new URLSearchParams(props.location.search);
const foo = params.get('foo'); // bar
Run Code Online (Sandbox Code Playgroud)

您可以在此处详细了解该决定

  • 不使用polyfill时不要使用URLSearchParams.截至2018年3月,Googlebot使用Chrome 41(https://developers.google.com/search/docs/guides/rendering),它不支持URLSearchParams,如果在关键路径中使用,您的应用将会中断(https:// caniuse. COM /#技艺= urlsearchparams). (6认同)
  • 上面的评论(从 2018 年春季开始)提到了 Googlebot 和 URLSearchParams 的不兼容性。然而一年后(2019年春季)情况有所改善。从那时起,Googlebot 使用最新的 Chromium,因此不再是避免 URLSearchParams 的理由。https://developers.google.com/search/blog/2019/05/the-new-evergreen-googlebot (2认同)

use*_*437 19

另一种有用的方法可能是URLSearchParams像这样使用开箱即用;

  let { search } = useLocation();

   const query = new URLSearchParams(search);
   const paramField = query.get('field');
   const paramValue = query.get('value');
Run Code Online (Sandbox Code Playgroud)

干净,可读,不需要模块。更多信息如下;


And*_*rew 9

根据他们的文档https://reactrouter.com/web/example/query-parameters你需要:

import { useLocation } from 'react-router-dom';

// A custom hook that builds on useLocation to parse
// the query string for you.
function useQuery() {
  return new URLSearchParams(useLocation().search);
}

function App() {
    const query = useQuery();
    console.log(query.get('queryYouAreLookingFor'));
}
Run Code Online (Sandbox Code Playgroud)


fis*_*ch2 7

给出的答案是可靠的.

如果你想使用qs模块而不是查询字符串(它们的流行度大致相同),这里是语法:

const query = qs.parse(props.location.search, {
  ignoreQueryPrefix: true
})
Run Code Online (Sandbox Code Playgroud)

ignoreQueryPrefix选项是忽略领先的问号.


kar*_*g01 5

可接受的答案很好用,但是如果您不想安装其他软件包,则可以使用以下方法:

getUrlParameter = (name) => {
    name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
    let regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
    let results = regex.exec(window.location.search);
    return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));
  };
Run Code Online (Sandbox Code Playgroud)

给定 http://www.google.co.in/?key=value

getUrlParameter('key');
Run Code Online (Sandbox Code Playgroud)

将返回 value


小智 5

我正在研究 react router v4 的参数,但他们没有将它用于 v4,不像 react router v2/3。我会留下另一个功能 - 也许有人会发现它有帮助。你只需要 es6 或普通的 javascript。

function parseQueryParams(query) {
  //You get a '?key=asdfghjkl1234567890&val=123&val2&val3=other'
  const queryArray = query.split('?')[1].split('&');
  let queryParams = {};
  for (let i = 0; i < queryArray.length; i++) {
    const [key, val] = queryArray[i].split('=');
    queryParams[key] = val ? val : true;
  }
  /* queryParams = 
     {
      key:"asdfghjkl1234567890",
      val:"123",
      val2:true,
      val3:"other"
     }
  */
  return queryParams;
}
Run Code Online (Sandbox Code Playgroud)

此外,此功能可以改进