【发布时间】:2020-01-02 11:06:42
【问题描述】:
我有以下路线:
<Route path="/userstream/:user" component={ Profile } param="stream" />
然后在我的组件中我以这种方式得到:user:this.props.match.params.user
但是,我如何才能从硬编码参数中获取 stream?
提前谢谢你。
【问题讨论】:
标签: reactjs react-router
我有以下路线:
<Route path="/userstream/:user" component={ Profile } param="stream" />
然后在我的组件中我以这种方式得到:user:this.props.match.params.user
但是,我如何才能从硬编码参数中获取 stream?
提前谢谢你。
【问题讨论】:
标签: reactjs react-router
一种方法是将其作为道具传递给组件:
<Route path="/userstream/:user" component={ (props) => <Profile {...props} param="stream" /> } />
编辑:为了完整起见,值得一提的是第二种方法,即使用render prop:
<Route path="/userstream/:user" render={ (props) => <Profile {...props} param="stream" /> } />
两种方法之间存在一些性能差异:react router difference between component and render。
【讨论】: