[英]How to get render submenus using current route in react-router v4?
My code looks similar to this : https://www.techiediaries.com/react-router-dom-v4/ 我的代码与此类似: https : //www.techiediaries.com/react-router-dom-v4/
<div className="base">
<header>
<p>React Router v4 Browser Example</p>
<nav>
<ul>
<li><Link to='/'>Home</Link></li>
<li><Link to='/about'>About</Link></li>
<li><Link to='/about/one'>AboutOne</Link></li>
<li><Link to='/about/two'>AboutTwo</Link></li>
</ul>
</nav>
</header>
<div className="container">
<Route path="/" exact component={HomePage} />
<Route path="/about" component={AboutPage} />
I want to be able to only show the links to AboutOne and AboutTwo if the user is on about
, about/one
, or about/two
(ie a page and its subpages). 我希望能够只显示关于AboutOne和AboutTwo的链接,如果用户处于
about
, about/one
或about/two
(即页面及其子页面)。 Unfortunately with <Link
s and <NavLink
s you can't manually set the active class (as far as I can tell), else I'd just do a visibility: visible
style for it. 不幸的是,使用
<Link
s和<NavLink
s,你无法手动设置活动类(据我所知),否则我只是做一个visibility: visible
它的visibility: visible
样式。 So I guess I have two problems that are preventing me from accomplishing this: first, I can't set when the Link
is active, and second, I don't know how to get the current route to determine if the user is on a page or its subpages. 所以我想我有两个问题阻止我完成这个:首先,我无法设置
Link
何时处于活动状态,其次,我不知道如何获取当前路由以确定用户是否在页面或其子页面。 Any ideas? 有任何想法吗?
You could wrap the submenu links in a if statement then use a callback to check if you are on the about path. 您可以将子菜单链接包装在if语句中,然后使用回调来检查您是否在about路径上。 You can check your path in React Router 4 using this.props.location.pathname
您可以使用this.props.location.pathname检查React Router 4中的路径
not sure what react style coding you are using but it could go like this 不确定你正在使用什么样的反应样式编码,但它可能会像这样
ifOnAboutPath() {
return this.props.location.pathname === 'about' ? true : false;
}
render() {
const showSubmenu = <ul><li><Link to='/about/one'>AboutOne</Link></li>
<li><Link to='/about/two'>AboutTwo</Link></li></ul>;
const subMenu = this.ifOnAboutPath.bind(this) ? showSubmenu : '';
<div className="base">
<header>
<p>React Router v4 Browser Example</p>
<nav>
<ul>
<li><Link to='/'>Home</Link></li>
<li><Link to='/about'>About</Link></li>
subMenu
</ul>
</nav>
</header>
<div className="container">
<Route path="/" exact component={HomePage} />
<Route path="/about" component={AboutPage} />
It appears I was looking at some janky react router docs that hadn't been updated. 看来我正在查看一些尚未更新的janky react路由器文档。 You can just the
isActive
prop. 你可以只是
isActive
道具。 So, for future googlers/bingers/duckduckgoers: 因此,对于未来的googlers / bingers / duckduckers:
<NavLink
to="/v/user"
isActive={(match, location) => {
console.log(match, location);
return location.pathname.startsWith("/v/user");
}}
className="nav-sub-link"
activeClassName="activeThing"
>
Submenu!
</NavLink>
.nav-sub-link:not(.activeThing) {
visibility: hidden;
}
.nav-sub-link {
visibility: visible;
//then some more styles
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.