Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link activeClassName not being applied for dynamic routes #3286

Closed
musbaig opened this issue Apr 12, 2016 · 10 comments
Closed

Link activeClassName not being applied for dynamic routes #3286

musbaig opened this issue Apr 12, 2016 · 10 comments
Labels

Comments

@musbaig
Copy link

musbaig commented Apr 12, 2016

Here is the issue I'm seeing. I have a typical navbar with several <Link>s linking to dynamically loaded (routed) components (Routes). Code-splitting for the dynamic routes is being handled with webpack and I'm also using react-router-redux to sync route state with my Redux store. (All deps are latest/greatest).

Now if I click on one of the links, the correct route is loaded, however, the <Link>'s activeClassName isn't applied. Clicking the link a second time, however, now applies the active class.

I'm wrapping <Link> in another component for styling purposes, and as such have tried using this.context.router.isActive(...); again, active styles and/or isActive flag are only triggered after the second click, not sure what is going on. Is this a bug in react-router? Any help would be greatly appreciated.

Note: this is only an issue with dynamic routes, if I were to turn them to a static ones, everything works as expected.

@musbaig
Copy link
Author

musbaig commented Apr 12, 2016

Btw, I also submitted this question on reactiflux#react-router channel, no one has responded yet, most discussion seems to be around react-router vs rrtr, politics.

@timdorr timdorr changed the title <Link>'s activeClassName not being applied for dynamic routes Link activeClassName not being applied for dynamic routes Apr 12, 2016
@timdorr
Copy link
Member

timdorr commented Apr 12, 2016

We have such tests for isActive. Do you have a test case we can check against?

@timdorr timdorr added the bug label Apr 12, 2016
@ryanflorence
Copy link
Member

@musbaig we can't really diagnose the problem without a test case, mind tossing one up on jsbin, codepen, a github repo, or a unit test in this project?

@musbaig
Copy link
Author

musbaig commented Apr 14, 2016

After fighting with this for several days, I finally figured out the root cause of the problem. react-redux's connect() was preventing my navbar, and subsequently navlinks, from rerendering on a location state change, since navbar was no longer reading said location state directly from the Redux store, as recommended here: https://github.com/reactjs/react-router-redux#how-do-i-access-router-state-in-a-container-component.
My workaround was to pass this.props.location.pathname as a props to the navbar to force the requisite rerender. Long story short, react-router was innocent, as a result I'm closing this issue!
These are not the droids you're looking for. Move along, nothing to see here. Sorry for the inconvenience. Keep up the good work.

@musbaig musbaig closed this as completed Apr 14, 2016
@taion
Copy link
Contributor

taion commented Apr 14, 2016

@musbaig You can also set pure: false in @connect to disable suppression of context updates. See also #470.

@musbaig
Copy link
Author

musbaig commented Apr 14, 2016

@taion oh wow, that's cooool, great tip, thank you soo much for following up!! Works perfectly, feels more natural and a lot less hacky, cheers!

@laustdeleuran
Copy link

laustdeleuran commented May 16, 2016

Perfect, this solved a similar problem with activeClassName for me. Thanks!

For newcomers, see documentation on { pure: false } here

@aleccool213
Copy link

aleccool213 commented May 19, 2016

@laustdeleuran That link should be somewhere in the react-router docs as it took me a while to find this :/

Anyways, thanks so much for the link. I am a noob to redux and react-router and this helped me immensely.

@laustdeleuran
Copy link

laustdeleuran commented May 19, 2016

@aleccool213 yeah, I'm pretty new at this too, and the documentation is not that easy to find your way around in. Spent some time looking for this too, before I found this thread.

@gaearon
Copy link
Contributor

gaearon commented May 22, 2016

This is fixed in React Router 3.0.0-alpha.1 and newer.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

7 participants