Reactjs 在除my React.js应用程序中的auth页面之外的每个页面上呈现导航栏

Reactjs 在除my React.js应用程序中的auth页面之外的每个页面上呈现导航栏,reactjs,Reactjs,我是react.js的新手,目前正在编写一个菜谱/食谱应用程序。目前,我被困在一些我认为很容易的事情上,但我似乎做不到。我正在努力使每个页面都有navbar组件,除了欢迎/登录/注册页面(全部在一个页面中)。My App.js返回以下内容: <BrowserRouter> <main className="main-content"> <Switch> <AuthContext.Provider value={{ tok

我是react.js的新手,目前正在编写一个菜谱/食谱应用程序。目前,我被困在一些我认为很容易的事情上,但我似乎做不到。我正在努力使每个页面都有navbar组件,除了欢迎/登录/注册页面(全部在一个页面中)。My App.js返回以下内容:

<BrowserRouter>
<main className="main-content">
  <Switch>
  <AuthContext.Provider 
    value={{
        token: this.state.token, 
        userId: this.state.userId, 
        login: this.login, 
        logout: this.logout
      }}>
          {!this.state.token && <Redirect from="/" to="/auth" exact/>}
          {!this.state.token && <Route path="/auth" component={AuthPage} />}
          {this.state.token && <Redirect from="/" to="/landing" exact/>}
          {this.state.token && <Redirect from="/auth" to="/landing" exact/>}}

     <MainNav>
        {this.state.token && <Route path="/landing" component={LandingPage}/>}
        {this.state.token && <Route path="/cookbook" component={CookbookPage}/>}
        {this.state.token && <Route path="/recipeLookup" component={RecipeLookupPage} />}       
      </MainNav>  
    </AuthContext.Provider>
  </Switch>
</main>
</BrowserRouter>

{!this.state.token&&}
{!this.state.token&&}
{this.state.token&&}
{this.state.token&&}
{this.state.token&&}
{this.state.token&&}
{this.state.token&&}
即使我用包装了我想要拥有导航的路线,导航也会显示在所有页面中,特别是身份验证页面。我试过一些方法,比如有两个标签,可以让navbar离开auth页面,但是其他路径都不起作用了。我还尝试了一些我已经研究过的其他解决方案,但我无法让它们在我的应用程序中工作


任何帮助都将不胜感激

当路由等于登录路由时,我想您可以将display prop更改为none。

如果您有auth route API,您可以检查localStorage

{
localStorage.getItem('token') ? (
  <MainNav>
     <Route path="/landing" component={LandingPage}/>
     <Route path="/cookbook" component={CookbookPage}/>
     <Route path="/recipeLookup" component={RecipeLookupPage} />       
  </MainNav> 
) : (null)
}
{
localStorage.getItem('token')(
):(空)
}
或者你可以用状态来控制它

{
this.state.token ? (
  <MainNav>
     <Route path="/landing" component={LandingPage}/>
     <Route path="/cookbook" component={CookbookPage}/>
     <Route path="/recipeLookup" component={RecipeLookupPage} />       
  </MainNav> 
) : (null)
}
{
这个。州。代币(
):(空)
}

您还可以为授权设置专用路由,如

搜索
authorized route
,查找反应路由器dom