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.

Active class not being added to NavLinks on active pages on v4.3.1

See original GitHub issue

Version

Upgraded react-router-dom from 4.2.2 → 4.3.1

Node v8.11.2 npm v5.6.0

With the following dependencies:

"babel-core": "^6.26.3",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"react": "^16.4.0",
"react-dom": "^16.4.0",
"react-router-dom": "^4.3.1",
"webpack": "^4.10.2",
"webpack-cli": "^3.0.1"

Steps to reproduce

After upgrading, the 'active' class is no longer applied to the <NavLink/> for active pages. I’ve tried explicitly adding the activeClassName prop to the component and it still doesn’t add the class the the element.

I haven’t been able to create a minimal verifiable example. Has anyone else seen this? Everything else works perfectly fine, builds like normal, but the active class isn’t added on the updated version.

Expected Behavior

It works as expected with v4.2.2 but not with v4.3.1 (or v4.3.0)

Additional Info

v4.2.2

Props being passed to NavLink:

activeClassName: "active"
ariaCurrent: "true"
children: Array[2]
className: "img-link"
exact: true
to: "/"

JSX and HTML:

<NavLink exact={true} to="/" className="img-link">  ↓ <a class="img-link active" aria-current="true" href="#/">


v4.3.1

Props being passed to NavLink:

activeClassName: "active"
aria-current: "page"
children: Array[2]
className: "img-link"
exact: true
to: "/"

JSX and HTML:

<NavLink exact={true} to="/" className="img-link">  ↓ <a class="img-link" href="#/">

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:25
  • Comments:44 (9 by maintainers)

github_iconTop GitHub Comments

54reactions
pshrmncommented, Jul 8, 2018

@dotspencer React Router’s path-to-regexp dependency is v1.7.0 while Express’s is v0.1.7. During installation, the path-to-regexp dependency for Express is installed before the one for React Router, so you end up with a node_modules that looks like this:

node_modules
  express
  path-to-regexp@0.1.7
  react-router
    node_modules
      path-to-regexp@1.7.0

Your Webpack configuration is set to resolve directly from the root node_modules, so when you build your project you are building it with the old path-to-regexp. The solution, based on this comment is to use a generalized node_modules reference in your Webpack config.

  resolve: {
    modules: [
      path.resolve('./src'),
-     path.resolve('./node_modules'),
+    'node_modules',
    ],
  },

If anyone else is running into this issue and uses Express + Webpack, can you please verify if this fixes your problem. (cc @dagda1)

10reactions
jameswysecommented, Jun 12, 2018

I’m also seeing this after upgrading from 4.2.2 to 4.3.1

Workaround: pass a function to the isActive prop to do the check manually, eg:

const isActive = (path, match, location) => !!(match || path === location.pathname);

<NavLink to="/my/path" 
         className="nav-link"
         activeClassName="nav-link--active"
         isActive={isActive.bind(this, '/my/path')}>
  Click Me 
</NavLink>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why NavLink does not add active class to dynamic route in ...
I am trying to understand why my NavLink elements do not add "active" class when dynamic route is specified. I have NavLink navigation...
Read more >
Active NavLink Classes with React Router - Ultimate Courses
From here we can an active class, or multiple classes, based on the isActive value. We can also supply inactive classes for when...
Read more >
export 'navlink' (imported as 'navlink') was not found in 'react ...
Attempted import error: 'Navlink' is not exported from 'react-router-dom' ... class not being added to NavLinks on active pages on v4.3.1#6201.
Read more >
Components - Bootstrap
Align nav links, forms, buttons, or text, using the .navbar-left or .navbar-right utility classes. Both classes will add a CSS float in the...
Read more >
Create an Active Page Link in Navbar - React Tutorial 18
FREE React Course (download & bonus content) - https://calcur.tech/free-react-courseReact YouTube Playlist ...
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