简体   繁体   English

react-router 索引路由始终处于活动状态

[英]react-router index route always active

I am trying to style my navigation by using the react activeClassName attribute.我正在尝试使用 react activeClassName属性来设置我的导航样式。 So far it works as expected, but there is a problem - my first nav links points to the root route(/).到目前为止它按预期工作,但有一个问题 - 我的第一个导航链接指向根路径 (/)。 So even when on another URL it will register that URL (for example /skills) and root as active (so 2 menu items get styled).因此,即使在另一个 URL 上,它也会将 URL(例如 /skills)和 root 注册为活动的(因此 2 个菜单项得到样式化)。

Is there an easy workaround for this?有一个简单的解决方法吗? Should I just define the Root route as something else (for example /home)?我是否应该将 Root 路由定义为其他内容(例如 /home)?

My header:我的header:

import React, { PropTypes } from 'react';
import { Link, IndexLink } from 'react-router';

const Header = () => {
    return (
        <div className="header">
            <div className="headerImage"></div>
            <ul className="headerNav">
                <li className="headerNavLink"><Link to="/" activeClassName="activeLink">introduction</Link></li>
                <li className="headerNavLink"><Link to="/skills" activeClassName="activeLink">skills</Link></li>
                <li className="headerNavLink"><Link to="/portfolio" activeClassName="activeLink">portfolio</Link></li>
            </ul>
        </div>
    );
};

export default Header;

routes.js:路线.js:

import React from 'react';
import { Route, IndexRoute } from 'react-router';
//Import app
import App from './components/App';
//Import pages
import IntroductionPage from './components/introduction/IntroductionPage';
import SkillsPage from './components/skills/SkillsPage';
import PortfolioPage from './components/portfolio/PortfolioPage';

//Define routes
export default (
    <Route path="/" component={App}>
        <IndexRoute component={IntroductionPage} />
        <Route path="skills" component={SkillsPage} />
        <Route path="portfolio" component={PortfolioPage} />
    </Route>
);

So to clarify, I would like my home route to not be active when on another route.所以澄清一下,我希望我的家乡路线在另一条路线上不活跃。

What am I doing wrong?我究竟做错了什么?

Thanks谢谢

You can use <IndexLink> or set onlyActiveOnIndex prop for links that are not to be highlighted when their children paths are active: 您可以使用<IndexLink>onlyActiveOnIndex其子路径处于活动状态时不突出显示的链接设置onlyActiveOnIndex prop:

<li className="headerNavLink"><IndexLink to="/" activeClassName="activeLink">introduction</IndexLink></li>

or 要么

<li className="headerNavLink"><Link to="/" activeClassName="activeLink" onlyActiveOnIndex>introduction</Link></li>

This changed again in v6.这在 v6 中再次发生了变化。

The prop exact has been replaced with end .道具exact已被替换为end

<NavLink end to="/profile">
  Profile
</NavLink>

Here is the React Router Dom docs reference这是React Router Dom 文档参考

Just like also to share this if you use "react-router-dom": "4.1.1", 如果您使用“react-router-dom”:“4.1.1”,也可以分享这个,

export const routes = <Index>
    <Route exact path='/' component={Home} />
    <Route path='/about' component={About} />
</Index>;

Then your navigation menu is 然后你的导航菜单是

<NavLink exact={true} activeClassName='active' to="/">
   Home
</NavLink>
<NavLink activeClassName='active' to="/bout">
    About
</NavLink>

Just specify also the "exact={true}" props for the NavLink and it should work as expected. 只需为NavLink指定“exact = {true}”道具,它应该按预期工作。

Thanks, Hope this helps. 谢谢,希望这会有所帮助。

In react-router-dom ^4.0 there's no IndexRoute or IndexLink . 在react-router-dom ^ 4.0中,没有IndexRoute或IndexLink Instead you need to specify the route path as exact and the child routes should be specified inside the <Route> 's component. 相反,您需要将路径路径指定为精确路径,并且应在<Route>的组件内指定子路由。 See this answer for more details: https://stackoverflow.com/a/43311025/2713729 有关更多详细信息,请参阅此答案: https//stackoverflow.com/a/43311025/2713729

I'd like to reference the docs for React Router ( react-router-dom , currently v5) and the sources . 我想引用React Routerreact-router-dom ,目前为v5)和源代码 的文档 This is the up-to-date way to implement this : 这是实现此目的的最新方法

exact : bool 确切地说 :bool

When true, the active class/style will only be applied if the location is matched exactly. 如果为true,则仅在位置完全匹配时才应用活动类/样式。

<NavLink exact to="/profile">
  Profile
</NavLink>

Here is a real world working example: 这是一个真实世界的工作示例:

<ul class="nav flex-column">
    <li class="nav-item">
      <NavLink className="nav-link" activeClassName="active" to="/" exact>Home</NavLink>
    </li>
    <li class="nav-item">
      <NavLink className="nav-link" activeClassName="active" to="/calendar">Calendar</NavLink>
    </li>
    <li class="nav-item">
      <NavLink className="nav-link" activeClassName="active" to="/page">Page</NavLink>
    </li>
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM