Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-router index route always active

I am trying to style my navigation by using the react activeClassName attribute. 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).

Is there an easy workaround for this? Should I just define the Root route as something else (for example /home)?

My 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:

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

like image 332
Miha Šušteršič Avatar asked Aug 28 '16 09:08

Miha Šušteršič


People also ask

How do I make my nav link active react?

The first method is to use the react-router-dom inbuilt method of NavLink instead of Link. The NavLink supports activeClassName which can help us assign active className to the link. Paste the below-updated code in your project Header. js file.

How do I restrict access to Route react?

The Route component from react-router is public by default but we can build upon it to make it restricted. We can add a restricted prop with a default value of false and use the condition if the user is authenticated and the route is restricted, then we redirect the user back to the Dashboard component.

What is the difference between NavLink and link?

The NavLink is used when you want to highlight a link as active. So, on every routing to a page, the link is highlighted according to the activeClassName . Link is for links that need no highlighting. And a is for external links.


1 Answers

I'd like to reference the docs for React Router (react-router-dom, currently v5) and the sources. This is the up-to-date way to implement this:

exact: bool

When true, the active class/style will only be applied if the location is matched exactly.

<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>
like image 58
sr9yar Avatar answered Sep 19 '22 05:09

sr9yar