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
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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With