在简书页面中Home页面有详情页在组件List中如下图:

react 路由获取参数 随笔 第1张

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

代码如下:

{list.map((item,index) =>{
return (
<Link to={'/detail/'+item.get('id')} key = {index}>
<ListItem >
<img className='pic' src={item.get('imgUrl')} alt=""/>
<ListInfo>
<h3 className='title'>{item.get('title')}</h3>
<p className='desc'>{item.get('desc')}</p>
</ListInfo>
</ListItem>
</Link>


)
})}


这里向路由传递参数
<Link to={'/detail/'+item.get('id')} key = {index}>
这样点击详情页不会显示页面内容需要在App.js中修改如下图:
react 路由获取参数 随笔 第2张

代码如下:<Route path = '/detail/:id' exact component={Detail}></Route>

在detail页面中获取传来的id入下图所示:

react 路由获取参数 随笔 第3张

在mapDispatch方法中定义一个获取详情页的方法,在方法中定义的actionCreaters定义一个ajax的方法的action并发送到store中,由详情页的reducer处理,如下图所示aciotnCreaters.getDetail()方法:

react 路由获取参数 随笔 第4张

因为页面引入redux-thunk,因此action可以是一个方法而不是对象,changeDetail方法创建action传递title和content参数。在detail的reducer中执行相应的代码如下所示:

react 路由获取参数 随笔 第5张

 

 

 

 
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄