gpt4 book ai didi

reactjs - 如何在 URL 更改时隐藏导航栏项目?

转载 作者:行者123 更新时间:2023-12-05 06:31:02 26 4
gpt4 key购买 nike

我有一个名为 Header 的导航栏组件,它几乎在我的 Web 应用程序的每个页面上都会被调用,现在我希望某些导航栏项目在打开某些页面时消失,就像我希望导航栏项目在 http://localhost:3000/stories 上消失一样。但必须显示在 http://localhost:3000/ ,我附上了图片。例如我想要“什么是值(value)”和“值(value)如何运作?”消失在/故事页面我已经在这些项目上编写了一个点击时的设置状态函数,但是当我点击故事导航项目时它们第二次工作。

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>

路由.js在 route :

    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 的组件包装您的路线,您在其中定义了 headerfooter 组件。这样您的 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

在header组件中添加navbar

关于reactjs - 如何在 URL 更改时隐藏导航栏项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51928191/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com