我的所有页面都有一个Sidebar
组件。
该Sidebar
具有“创建新帖子”按钮。
单击后,按钮将重定向到NewDiary
组件。
<HashRouter>
<div className='app'>
<div id='wrapper'>
<div id='content' className='mobileUI-main-content'>
<div id='content-inner'>
<Match exactly pattern='/entry/new' component={NewDiary} />
<Match exactly pattern='/entry/:id' component={DiaryDetails} />
<Match exactly pattern='/' component={LandingPage} />
</div>
</div>
<Sidebar />
</div>
</div>
</HashRouter>
如何使
Sidebar
知道我已打开NewDiary
并隐藏按钮?我没有使用Redux,我也想避免使用它,因为我的应用程序非常小。
最佳答案
尝试从道具中获取当前路径,并在NewDiary页面中使用路径或路由器进行条件设置。
const currentPath = this.props.location.pathname
return (
<div>
{if currentPath === '/entry/new' ?<YourButtonComponent /> : null}
</div>
)