reactjs - 如何在 URL 更改时隐藏导航栏项目?
问题描述
我有一个名为 Header 的导航栏组件,它在我的网络应用程序的几乎每个页面上都被调用,现在我希望一些导航栏项目在打开某些页面时消失,比如我希望导航项目在http://localhost:3000/stories上消失但必须在http://localhost:3000/上显示,我附上了图片。例如,我想要“什么是价值”和“价值如何运作?” 在/stories 页面上消失我在单击时为这些项目编写了一个设置状态函数,但是当我单击故事导航项目时它们第二次起作用。
operation()
{
this.setState({showme:false})
}
<Navbar className="fixed-top navbar-custom" color="white" light expand="lg">
<Container>
<NavbarBrand tag={Link} to='/'>
<img src={logo} alt="logo" className="logo" />
</NavbarBrand>
<NavbarToggler onClick={this.toggle} />
<Collapse isOpen={this.state.isOpen} navbar>
{ this.state.showme?
<Nav className="mr-auto" navbar style={{cursor:'pointer'}}>
<NavItem>
<NavLink onClick={this.scrollToTop} className = "navlink-custom">What is Valu?</NavLink>
</NavItem>
<NavItem>
<NavLink onClick={this.scrollTo} className = "navlink-custom">How Valu work ?</NavLink>
</NavItem>
</Nav>
:null
}
<Nav className="ml-auto" navbar >
<NavItem>
<NavLink onClick={this.operation} tag={Link} to='/stories' className = "navlink-custom">Stories</NavLink>
</NavItem>
<NavItem >
<NavLink tag={Link} to='/aboutus' className = "navlink-custom" Link to="/aboutus">About us</NavLink>
</NavItem>
<NavItem>
<Link to="/signup">
<button className="btn-login">
<div className="login">Register/login</div>
</button>{' '}
</Link>
</NavItem>
</Nav>
</Collapse>
</Container>
</Navbar>
Routes.js 在路由中:
const AppRouter = () =>
{
return (
<Router>
<Switch>
<Route exact path='/' component={App}/>
<Route path='/howvaluworks' component={HowValuWorks} />
<Route path='/Footer' component={footer} />
<Route path='/aboutus' component={AboutUs} />
<Route path='/login' component={loginform}/>
<Route path='/signup' component={signupform}/>
<Route path='/signup' component={signupform}/>
<Route path='/profile-tutorial' component={profiletutorial}/>
<Route path='/profile-account' component={profileaccount}/>
<Route path='/stories' component={stories}/>
<Route path='/profilelaunch' component={profilelaunch}/>
)};
解决方案
根据 中的路线位置设置条件componentWillReceiveProps
。
constructor(props){
super(props);
this.state = {
hideValu : 1
};
this.changeNavItem = this.changeNavItem.bind(this);
}
componentDidMount(){
this.changeNavItem(this.props.location.pathname);
}
componentWillReceiveProps(nextProps){
if(this.props.location.pathname !== nextProps.location.pathname){
this.changeNavItem(nextProps.location.pathname);
}
}
changeNavItem(currentRoute){
if(currentRoute == "\stories"){
this.setState({
hideValu : 0
});
}
}
在导航栏中,
{ this.state.showme? <Nav className="mr-auto" navbar style={{cursor:'pointer'}}>
this.state.hideValu && <div>
<NavItem>
<NavLink onClick={this.scrollToTop} className = "navlink-custom">What is
Valu?</NavLink>
</NavItem>
<NavItem>
<NavLink onClick={this.scrollTo} className = "navlink-custom">How Valu
work ?
</NavLink>
</NavItem>
</div>
</Nav>
:null
}
更新
MainLayout
用你定义你的组件header
和组件包装你的路由。footer
这样你的 props.location 值就会得到更新,你就可以访问它了。
<Router>
<Switch>
<MainLayout>
<Route exact path='/' component={App}/>
<Route path='/howvaluworks' component={HowValuWorks} />
<Route path='/Footer' component={footer} />
<Route path='/aboutus' component={AboutUs} />
<Route path='/login' component={loginform}/>
<Route path='/signup' component={signupform}/>
<Route path='/profile-tutorial' component={profiletutorial}/>
<Route path='/profile-account' component={profileaccount}/>
<Route path='/stories' component={stories}/>
<Route path='/profilelaunch' component={profilelaunch}/>
<Route path='/draft' component={draft}/>
<Route path='/dashboard' component={dashboard}/>
<Route path='/launchsurvey' component={launchsurvey}/>
</MainLayout>
</Switch>
</Router>
MainLayout.js
import React from "react"
import Header from '../containers/Header';
import Footer from "./Footer"
class MainLayout extends React.Component{
render() {
return(
<div>
<Header />
<div className="appLayout">
{ this.props.children }
</div>
<Footer />
</div>
);
}
}
export default MainLayout
添加也在navbar
头组件中添加
推荐阅读
- bash - 如何使用通配符包含未知数量的前导零
- ansible - ansible:根据布尔主机变量跳转到块的救援部分
- javascript - 编译器失败 - 找不到“AppModule”的 NgModule 元数据中的错误
- c# - Azure 上的 .Net Core 后台服务作为 WebJob 不调用 StopAsync
- javascript - 如何在反应中使用 where 条件选择单个 Firebase 实时数据库数据?
- json - 尝试通过 javax.ws.rs 将 POST 请求作为 Json 发送。org.glassfish.jersey.model.internal.CommonConfig。
例外 - docker - 使用 golang 在 GCR 中列出 docker 存储库
- javascript - Express 4.17 body-Parser 已弃用
- c# - C# - 无法将 var 转换为 List
- javascript-objects - 这段代码有什么问题?我没有定义