学习react路由传参(仅供学习参考)

1、params传参

使用params传参需要再注册路由时使用占位符接收参数。

路由链接:<Link to="{`/home/${id}/${name}`}"></Link>

注册路由:<Route path="/home/:id/:name" component={home}/>

接收参数:const{id,name} = this.props.match.params

2、search参数

使用search传参可以正常注册路由,在接收时可以通过插件querystring来解析参数(也可以自己处理参数)

路由链接:<Link to="{`/home/?id=${id}&name=${name}`}"></Link>

注册路由:<Route path="/home" component={home}/>

接收参数:

import qs from 'querystring'

const {search}= this.props.location

const {id,name} = qs.parse(search)

2、state参数(在路径上不会体现传的参数,但刷新页面也能保留参数,前俩者亦能)

使用search传参可以正常注册路由,但在传参时使用对象进行传参

路由链接:<Link to="{{pathname:'/home',state:{id,name}}}"></Link>

注册路由:<Route path="/home" component={home}/>

接收参数:

const {id,name}= this.props.location.state||{}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值