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