隐藏特定页面上的组件

问题描述 投票:0回答:1

我有这种结构:

        <Router onUpdate= {scrollToTop} history={history}>
        <div>
        <Navbar/> 

          <ScrollToTopRoute exact path="/" component={home} />
          <ScrollToTopRoute path="/fund" component={fund} />
          <ScrollToTopRoute path="/browseideas" component={browseideas} />
          <ScrollToTopRoute path="/earnwithus" component={earnwithus} />
          <ScrollToTopRoute path="/register" component={RegisterPage} />
          <ScrollToTopRoute path="/login" component={LoginPage} />

          <ScrollToTopRoute path="/idea" component={idea} />

          <ScrollToTopRoute path="/lightning" component={Lightning} />
          <ScrollToTopRoute path="/storm" component={Storm} />
          <ScrollToTopRoute path ="/increase" component ={increase}/>
          <ScrollToTopRoute path ="/policy" component ={policyprivacy}/>

          <PrivateRoute path ="/homepage" component ={homepage}/>

          <PrivateRoute path ="/activehedges" component ={ActiveHedges}/>
          <PrivateRoute path ="/userprofile" component ={UserProfile}/>
          <PrivateRoute path ="/myfunds" component ={MyFunds}/>

          <PrivateRoute path ="/deposit" component ={deposit}/>
          <PrivateRoute path ="/withdraw" component ={withdraw}/>

        <Footer/>
        </div>
      </Router>

Navbar&Footer应该在每个页面上,而不是RegisterPage和LoginPage。

如何以与添加到每个页面不同的方式隐藏<Navbar/> <Footer/>,这是推荐的做法吗?

reactjs hide router
1个回答
2
投票

您可以使用某些默认状态,设置类似registered:false

在您的父组件中:

this.state = { registered: false };

然后,在<Navbar/><Footer/>中,检查注册是否为false或true:

if(!this.state.registered){
   // Hide component
} else {
   // Show component
}

当然,一旦用户注册,您需要将注册设置为true

© www.soinside.com 2019 - 2024. All rights reserved.