在 React Router 中,使用浏览器历史推送时如何传递路由参数? [英] In React Router, how do you pass route parameters when using browser history push?

查看:53
本文介绍了在 React Router 中,使用浏览器历史推送时如何传递路由参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用较新版本的 React Router,如果您使用 browserHistory.push() 进行转换,如何将参数传递给要转换到的路由?如果 browserHistory 不允许这样做,我愿意使用其他方式进行转换.我看到在 React Router 中我最顶层的子组件中有 this.props.routeParams 但我似乎无法在转换时用我想要的上一个路由的值填充它.

解决方案

现在你将有这样的事情

history.push({路径名:'/关于',搜索:'?the=search',状态:{一些:'状态'}})

这是 API 文档的链接>

Using the newer version of React Router, how do you pass parameters to the route you are transitioning to if you are transitioning using the browserHistory.push()? I am open to using some other way to transitioning if browserHistory does not allow this. I see that in my top most child component in React Router there is this.props.routeParams but I can't seem to get this populated with values I want from the previous route when transitioning.

解决方案

Now you will have do something like this

history.push({
  pathname: '/about',
  search: '?the=search',
  state: { some: 'state' }
})

Here is the link for API documentation

这篇关于在 React Router 中,使用浏览器历史推送时如何传递路由参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆