隐藏/显示导航栏 ReactJS
Hide / Show Navbar ReactJS
我在尝试隐藏登录页面上的导航栏时遇到问题,但我不知道该怎么做。
你可以在这里看到我的代码:
render() {
return (
<Router>
<div >
<Nav />
<button type="button" className="form-submit_logout" onClick=
{this.handleLogout.bind(this)}>Logout</button>
<Route path="/" exact component={Login}/>
<Route path="/ChooseRole" exact component={ChooseRole}/>
<Route path="/DashboardGeek" exact component= .
{DashboardGeek}/>
<Route path="/DashboardAdmin" exact component= .
{DashboardAdmin}/>
</div>
</Router>
请帮我解决这个问题。谢谢
这是因为您有根级别的组件,所以它会呈现在所有页面中。
尝试将其作为子组件分别包含在 ChooseRole
、DashboardGeek
和 DashboardAdmin
组件中。那应该可以解决问题。
有几种解决方法,这取决于您的应用程序的规模,但我认为最简单的方法如下。
我假设您在应用程序或全局状态中使用布尔值或用户对象?尝试添加一个布尔值来渲染导航,如:{this.state.loggedIn ? <Nav /> : ''}
您还可以生成一个 'AppTemplate' 组件,将路由作为 props.children
传递或将其包含在每个页面中。
我在尝试隐藏登录页面上的导航栏时遇到问题,但我不知道该怎么做。
你可以在这里看到我的代码:
render() {
return (
<Router>
<div >
<Nav />
<button type="button" className="form-submit_logout" onClick=
{this.handleLogout.bind(this)}>Logout</button>
<Route path="/" exact component={Login}/>
<Route path="/ChooseRole" exact component={ChooseRole}/>
<Route path="/DashboardGeek" exact component= .
{DashboardGeek}/>
<Route path="/DashboardAdmin" exact component= .
{DashboardAdmin}/>
</div>
</Router>
请帮我解决这个问题。谢谢
这是因为您有根级别的组件,所以它会呈现在所有页面中。
尝试将其作为子组件分别包含在 ChooseRole
、DashboardGeek
和 DashboardAdmin
组件中。那应该可以解决问题。
有几种解决方法,这取决于您的应用程序的规模,但我认为最简单的方法如下。
我假设您在应用程序或全局状态中使用布尔值或用户对象?尝试添加一个布尔值来渲染导航,如:{this.state.loggedIn ? <Nav /> : ''}
您还可以生成一个 'AppTemplate' 组件,将路由作为 props.children
传递或将其包含在每个页面中。