-
-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Active state not working correctly on links with query strings #3293
Comments
This is operating as designed and tested. We expect the full location to match, including the query string. It's actually more useful this way. Otherwise, how would you go about having links to the Open / Close state and mark either as the currently active one? |
I'd have to solve that problem myself anyway, and already have (this isn't the first time I've built a complex faceted search feature). Because as soon as you care about more than one query parameter (which I do), whether a filter is "active" can't be determined without looking at the individual parameters - exact marching on the full URL won't work. Aside: if you're asking me a follow-up question, shouldn't you wait for my reply before closing the ticket?
|
It's tricky here. Ultimately I think we have a relatively simple API in I'm not sure what we can necessarily do here, though. |
For every person who wants it to work the way discussed here (if the segment of the query matches, it's active) there's another who doesn't want that to be active, but only if the entire query matches. In these situations, we ask "what does the browser do?". a:visited { color: purple; } <a href="?foo=bar">bar</a>
<a href="?foo=baz">baz</a> If you visit We think this is the path of least surprise. |
It was rhetorical. Sorry if it came off snarky. There's really a few different ways to define "active"-ness of a URL. We've chosen one that works well for most people and the decision is made. There's not really anything actionable here. What would make a better issue is the discussion and consideration of more expressive "active" checking. For instance, having props like |
@ryanflorence I don't think we should automatically use the state of visited links as an example to follow.. Active route functionality has no existing semantics in browsers, mapping it to something else that may not be analogous is an error in my mind. This is functionality that can be ignored and re-implemented by users of react-router (I assume, since i'm being told this is what I have to do). The fact that it can be implemented on top of the router, suggests to me that the only way it can justify being in the core is if everyone works in a way that you can argue solves nearly everyone's problems. Otherwise it's dead file-size that we can't opt out of. To that end, I'd like to ask what use cases active route indicators actually address, two spring to mind, and I'm not convinced they're fulfilled by the current implementation:
(Note, I appreciate my mentioning of best practices is somewhat nebulous, but i've been a URL design nut for about 10 years, and haven't seen much to contradict what i'm saying) Basically, I think neither of these use cases are wholly supported by the current solution, but I strongly suspect the first one is a more universal requirement for building sites, and it's also the use case closest to being fully supported - with the exception of the edge case I've presented in this ticket. If there are common use cases i've missed, i'd love to hear them - if i'm wrong, I want to know why :) (Another aside: should this whole feature have a different name? Because "active" already has a defined meaning when it comes to link states.) |
I can tell you that as a user of this library, the first time I hit the functionality for determining active state for queries on Additionally, at this point, it'd be quite expensive for us to introduce a breaking change to the semantics of active state on query state on As such, why don't we talk about ways to better support your use case? One option I'm considering is backporting a few changes from React-Router-Bootstrap into here. The one change relevant to you would be to accept an Another option might be to factor out reusable logic (the click handler, mostly) in Thoughts? |
I'm actually a little confused, because when I discussed this with you in Discord a few days ago, you indicated that the behaviour I'm expecting here is what was already supported, however that's not important. I still want to know what use cases I'm missing, because if we're going for unsurprising, I find the current implementation surprising. But in the interests of this conversation going somewhere, let's look at your suggestions. I think any solution has meet a couple of criteria:
I don't mind writing my own Link component, as long as it's trivial to do so and i'm not copy-pasting logic. I already wrap every DOM and 3rd party component to inject CSS Modules, so this isn't creating work I don't already do. Conceptually it feels cleaner than adding yet another prop to In terms of determining active route, I like @timdorr's suggestions, but I'd prefer being able to perform those checks via the router or history objects rather than as className props (who knows, it may affect something other than what classes get applied to the link). It feels like I need some of the logic that React Router provides, but some way of specifying which parts of the URL I care about when it comes. |
I'm not sure what you mean. If you think about what's involved here, this is going to look effectively like calling This line of code: https://github.com/reactjs/react-router/blob/b5175b15bf0999e5fb151f57a8126aca3537a7c9/modules/Link.js#L119 is all that's involved in the link active state determination. Nothing else would change here, so code size ought not be a concern here. |
What else is |
It's exposed as an API on the router. I use it directly in a few places (via |
Okay, based on what you've said, it seems the right approach is pulling out the bulk of |
Which parts of |
Well it seems i'd have to write my own version of of the render method to create a different descriptor, which looks like a lot of code copying for such a tiny change. |
The idea would be that we accept |
That works, and if it's the simplest approach to implement I'm happy. I'm not convinced it's the best solution, but i'd live with it for now. |
Scenario
I have a link in my global header to page with a filterable list of objects. The link includes a query parameter, because the starting state of the list is to already have a filter applied. The link looks something like this:
When I click the link and the page loads, the active class is correctly added to the link.
Problem
When I change the filters, e.g. changing the path to just
/tickets
or/tickets?status=closed
the active state is removed from the links.What I expect
I expect query strings to be ignored for all consideration of what is the active path. Links behave as expected if they link to the naked pathname, rather than one with a query string (i.e adding filters preserves the active behaviour), but break when the link itself contains a query.
Versions affected
Only observed with 2.0+, but no reason to assume the problem didn't exist before.
The text was updated successfully, but these errors were encountered: