question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add inactiveClassName to NavLink (feature request)

See original GitHub issue

When using a global/functional CSS strategy like Tachyons is it critical we do not rely on selector specificity and className order. At present, however, to style active and inactive NavLinks we do this:

<NavLink className="f5 black" activeClassName="red" />

Which renders as

<a class="f5 black" /> // <-- inactive
<a class="f5 black red" /> // <-- active

This happens to work because .red is after .black in the Tachyons style sheet. But if I’d chosen .navy instead of .black - which happens to be after .red - then the active style does nothing.

As you can see this is inherently brittle and furthermore browsers are not required by the W3C spec to honour class order. You can get around this by creating custom css classes and invoking !important or increased specificity but this breaks a core principle of functional css.

Preferred would be:

<NavLink className="f5" activeClassName="red" inactiveClassName="black" />

Which would render as

<a class="f5 black" /> // <-- inactive
<a class="f5 red" /> // <-- active

giving you the results you’d expect.

Side note: from a general point of view, I’ve found that adding classes - as opposed to switching them - generally isn’t scalable in a wide variety of cases not just Tachyons.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
JofArnoldcommented, Feb 1, 2018

If they don’t make the mod, making your own component involves changing a couple of lines. Here’s roughly [1] what I use https://gist.github.com/JofArnold/21d8176377ef5a7d55a7b9221b852e63 Also enhanced to take a child function so you can do stuff like this:

<NavLink {...someProps}>
  {({ isActive }) =>
    isActive ? <span>Active :)</span> : <span>Inactive :(</span>}
</NavLink>;

[1] I say “roughly” in the sense I edited the code directly in the gist without testing so apologies if it doesn’t work

1reaction
JofArnoldcommented, Sep 24, 2017

Which I’m saying an anti-pattern. Additive class names is a code smell - especially in React - and relying on style sheet order is fragile and not something the developer always has control over.

It doesn’t matter what you name them

.defaultStyles {
  color: black;
}

.activeStyles {
  color: red;
}

it still relies on style sheet selector order or specificity. If you cannot control the selector order in the style sheet (e.g. you’re using Tachyons or Bootstrap from a CDN) then you’re out of luck. To get around this if you’re using scss versions of those stylesheets you control the order whilst still referring to the canonical styles by using @extend

.inactive {
  @extend .black;
}

.active {
  @extend .red;
}

but that’s all you can do.

Can you please clarify your use of “just” with an example that doesn’t require modifying Bootstrap or Tachyons.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass activeClassName to parent <li> #2999 - GitHub
Go look at the source of Link for inspiration. When I sweep through the docs for 2.0 I'll have an example in there....
Read more >
Add an active className to the link using React Router
The NavLink supports activeClassName which can help us assign active className to the link. Paste the below-updated code in your project Header.
Read more >
Active NavLink Classes with React Router - Ultimate Courses
This function gets invoked on every route change, which makes it the perfect place to add an active class (or add an inactive...
Read more >
How do I add an active class to a Link from React Router?
First import it import { NavLink } from 'react-router-dom'; · Use an activeClassName to get the active class property. · Style your class...
Read more >
React Router 6 - What Changed & Upgrading Guide - YouTube
React Router v6 (stable!) was released and it's a great improvement over v5! This video covers all the important new features AND dives...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found