react-router: Active class not being added to NavLinks on active pages on v4.3.1
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="#/">
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 25
- Comments: 44 (9 by maintainers)
Commits related to this issue
- fix nav active link - correctif dans la config webpack qui entrainait un bug dans react-router (https://github.com/ReactTraining/react-router/issues/6201) — committed to mathieu-bellange/project-nz by mathieu-bellange 6 years ago
- Fix issue with wrong NavLink's work Original issue: https://github.com/ReactTraining/react-router/issues/6201 — committed to Saritasa/react-app-core by NumminorihSF 5 years ago
@dotspencer React Router’s
path-to-regexp
dependency isv1.7.0
while Express’s isv0.1.7
. During installation, thepath-to-regexp
dependency for Express is installed before the one for React Router, so you end up with anode_modules
that looks like this: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 oldpath-to-regexp
. The solution, based on this comment is to use a generalizednode_modules
reference in your Webpack config.If anyone else is running into this issue and uses Express + Webpack, can you please verify if this fixes your problem. (cc @dagda1)
I’m also seeing this after upgrading from
4.2.2
to4.3.1
Workaround: pass a function to the
isActive
prop to do the check manually, eg:I’m not sure if this is related to this issue. But when I use relative path, the
activeClassName
is not applied.For example,
Active class is not applied.
Active class is applied.
The url routes to the correct path but no active class.
@pshrmn I’m pretty sure this is only ssr related
@jonthomp I just took a closer look at
tabler-react
and the<Nav.Item>
is a pure component, so when the location changes, the<NavLink>
s aren’t being re-rendered (the blocked updates problem). That will go away when RR adopts the new context, but isn’t the same issue as other people are experiencing.To everyone else in this thread: can someone please put together a small repo or sandbox?
Same issue here with 4.3.1, I have an ssr app and the class is being added from the code rendered on the server but not when changing views on the client.
I rolled back to 4.2.2 and all is now good.
I tried to debug and match was always null in
NavLink
at this location.@eulow that is the behviour I experienced, I implemented my own isActive and noticed no matter what I did a match was not found. I even ran the latest path-to-regexp locally through the node console with the same arguments and a match was found.
Are you using react-scripts or create react app?
I think the
path-to-regexp
is still getting clobbered by webpack-dev-server’s express reference in some circumstances.Create react app is making
react-scripts
a dependency and not a devDependency.This only happened on a production build.
my fix was
npm i path-to-regexp@1.7.0 -S
A match was then magically found.
I thought this might be fixed in v5 but it is not. I still have to use the workaround shown above by apvilkko
Until 4.4 is stable, I’m using this workaround… It’s not ideal since I have to specify the path twice when calling the component.
@gothbarbie it seams that this issue is fixed with 4.4.0-beta builds (I have switched to 4.4.0-beta.6)
don’t update to the latest package. update to
npm i path-to-regexp@1.7.0 -S
Hey, I was wrong. Again I ran into the same problem in a different project. This time I found the real fix. Update your “path-to-regexp” package to latest as mentioned by @dagda1.
Hi @pshrmn , is there a way to get this worked if we not using webpack.config but instead using create-react-app to setup the react app. is ejecting the app only way to this fixed in this case ?
I ran into the same issue but could not use the generalized
'node_modules'
workaround. Instead, I hard-codedpath-to-regexp
to always resolve to the newer version via alias.Absolutely worked for me also. I would never have suspected that.
I have a mono repo. Still not entirely sure how this fixes it for the monorepo.
I created a small repo that reproduces the bug: https://github.com/dotspencer/react-router-bug
One curious thing I discovered is that the problem goes away when I move the express package from
"dependencies"
to"devDependencies"
inpackage.json
.@pshrmn @dagda1 the issue is not SSR related for us
I think its related to https://github.com/ReactTraining/react-router/issues/6193.