我的所有页面都有一个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>
   )

10-07 13:22
查看更多