从 react-router 哈希片段获取查询参数

IT技术 javascript reactjs client-side react-router client-side-scripting
2021-04-06 11:29:36

我在客户端为我的应用程序使用 react 和 react-router 。我似乎无法弄清楚如何从 url 获取以下查询参数,例如:

http://xmen.database/search#/?status=APPROVED&page=1&limit=20

我的路线看起来像这样(我知道路径完全错误):

var routes = (
<Route>
    <DefaultRoute handler={SearchDisplay}/>
    <Route name="search" path="?status=:status&page=:page&limit=:limit" handler={SearchDisplay}/>
    <Route name="xmen" path="candidate/:accountId" handler={XmenDisplay}/>
</Route>
);

我的路线工作正常,但我不确定如何格式化路径以获取我想要的参数。感谢您对此的任何帮助!

6个回答

注意:从评论中复制/粘贴。一定要喜欢原帖!

用 es6 编写并使用 react 0.14.6 / react-router 2.0.0-rc5。我使用此命令在我的组件中查找查询参数:

this.props.location.query

它在 url 中创建所有可用查询参数的哈希。

更新:

对于 React-Router v4,请参阅此答案基本上,用于this.props.location.search获取查询字符串并使用query-string包或URLSearchParams 进行解析

const params = new URLSearchParams(paramsString); 
const tags = params.get('tags');
query似乎在 React Router 4 中消失了。github.com/ReactTraining/react- router/ issues/...
2021-06-03 11:29:36
Per Peter,这个例子已经过时了:见stackoverflow.com/a/35005474/298073
2021-06-13 11:29:36
它不再是 React-router 1.x 然后是 2.x ////
2021-06-17 11:29:36

以上答案在react-router v4. 这是我为解决问题所做的工作-

首先安装解析所需的查询字符串

npm install -save query-string

现在在路由组件中,您可以像这样访问未解析的查询字符串

this.props.location.search

您可以通过登录控制台进行交叉检查。

最后解析访问查询参数

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

所以如果查询就像 ?hello=world

console.log(parsed.hello) 会登录 world

或者没有 lib:(const query = new URLSearchParams(props.location.search); console.log(query.get('hello'));旧浏览器需要 polyfill)。
2021-05-27 11:29:36
这应该在我构建应用程序时起作用,对吗?
2021-05-29 11:29:36

旧(v4 之前):

用 es6 编写并使用 react 0.14.6 / react-router 2.0.0-rc5。我使用此命令在我的组件中查找查询参数:

this.props.location.query

它在 url 中创建所有可用查询参数的哈希。

更新(react-router v4+):

React Router 4 中的 this.props.location.query 已被删除(目前使用 v4.1.1)更多关于这里问题的信息:https : //github.com/ReactTraining/react-router/issues/4410

看起来他们希望你使用自己的方法来解析查询参数,目前使用这个库来填补空白:https : //github.com/sindresorhus/query-string

嘿@ThomasModeneis,这是您的路由器正在渲染的最顶层父组件吗?或者它是一个子组件?如果我没记错的话,您必须将 this.props.location.query 从路由器呈现的父组件传递到您的子组件中,这是我唯一能想到的。
2021-05-29 11:29:36
我已经用 React 0.14.8 测试过,你的答案不起作用渲染:错误TypeError: Cannot read property 'location' of undefined::|
2021-06-21 11:29:36

更新 2017.12.25

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

网址喜欢

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

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

具有查询字符串依赖性:

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);

结果:

2 {sort: "name"} home


"react-router": "^2.4.1"

网址喜欢 http://localhost:8080/react-router01/1?name=novaline&age=26

const queryParams = this.props.location.query;

queryParams 是一个包含查询参数的对象: {name: novaline, age: 26}

如果你有这样的东西:(http://localhost:8090/#access_token=PGqsashgJdrZoU6hV8mWAzwlXkJZO8ImDcn&expires_in=7200&token_type=bearer即没有/之后#)那么对于反应路由器 v4,你应该使用location.hash而不是location.search
2021-06-08 11:29:36
"react-router-dom": "^5.0.0",

您不需要添加任何其他module,只需在具有如下 url 地址的组件中即可:

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

您可以尝试以下简单代码:

    const search =this.props.location.search;
    const params = new URLSearchParams(search);
    const authority = params.get('authority'); //