欢迎来到码农权的博客 MaNongGeGe.|

ReactRouter-参数传递与获取参数

参数传递与获取参数

1. 动态路由传参 *
  • 前提配置路由,留好占位
// 1.配置占位
<HashRouter>
  <Switch>
     <Route path="/xxx/:id"></Route> //留好占位
  </Switch>
</HashRouter>

// 2.参数传递
import useHistory from 'react-router-dom'
const historty = useHistory()
let id = 6666;
history.push(`/xxx/${id}`);

// 3.接收参数
let param = props.match.param.id;
console.log(param)
2. query传参
  • 路径中不显示参数
// 1.参数传递
import useHistory from 'react-router-dom'
const historty = useHistory()

history.push({ pathname: '/xxxx', query: { id: 666} });

// 2.接收参数
console.log(props.location.query.id)
3.state传参
// 1.参数传递
import useHistory from 'react-router-dom'
const historty = useHistory()

history.push({ pathname: '/xxxx', state: { id: 666} });

// 2.接收参数
console.log(props.location.state.id)

本文作者:HuangBingQuan

本文链接:https://www.cnblogs.com/bingquan1/p/16954093.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   HuangBingQuan  阅读(657)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 不将就 码农权
  2. 2 Bleeding Love 码农权
  3. 3 想你念你 码农权
Bleeding Love - 码农权
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

Not available