我的routes.jsx看起来就像
return (
<Route path='/' component={Main}>
<IndexRoute
component={Home}
onEnter={onSearchEnter}
onChange={(prevState, nextState) => onSearchEnter(nextState)}
/>
<Route path='privacy' component={Privacy} />
<Route path='terms' component={Terms} />
<Route path='dmca' component={DMCA} />
<Route path='clips/:clip' component={Clip} />
</Route>
)在我的Main中,我有如下内容:
render () {
return (
<div className={`main__container ${get(this, 'props.children.type.parentClassName')}`}>
{(() => {
return (
<Nav
downloadLinks={this.props.downloadLinks}
search={this.props.search}
/>
)
})()}但是,如果处于Nav路由,则不希望呈现clips/:clip组件。
我用的是react-router
发布于 2016-12-16 01:18:45
您应该使用通过react路由器传递给您的主容器的位置支柱。
它是一个对象,您可以在其中找到当前路径名。我会这样做:
isNavVisible = (location) => {
return location.pathname.split('/')[1] !== 'clips';
// location.pathname would be '/clips/someClipID'
// => location.pathname.split('/') would be ["", "clips", "someClipID"]
}
render () {
return (
<div className={`main__container ${get(this, 'props.children.type.parentClassName')}`}>
{this.isNavVisible(this.props.location) &&
<Nav
downloadLinks={this.props.downloadLinks}
search={this.props.search}
/>
}
...
}https://stackoverflow.com/questions/41175656
复制相似问题