-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Relative <Link to> #2172
Comments
Yes, I agree. This is a feature I'd love to support. |
I do like it, however I wouldn't make it a default Sometimes I just like that "I don't need to think" what path my component is at and I just use the absolute path (as it is now). But I do agree that it would be useful to support relative path. |
Ideally it would work just like |
oh yeah! I might work on that tomorrow |
👍 this would make routes easier to compose too without having to know where they're placed, e.g. order form with links to the different stages of it can be reused in multiple contexts:
can just be declared as (
|
FWIW, relative paths would also be handy on |
I don't think I agree here. |
Ahh so would this actually make the links relative to route that they're declared in instead of the current route? If not, while pushState and replaceState can be called from practically anywhere, that's still within a context (a browser |
In addition to relative /* replaceState would be history.replaceState or something similar, this just wraps it */
function addQuery(replaceState, path, query = {}, next = {}) {
const nextQuery = assign({}, query, next);
replaceState(null, path, nextQuery);
}; And I think the addition of new query keys could also be made easier. I originally mentioned this in #2226 but not sure if this issue might also cover it. I agree that it's also useful to be able to wipe it clean, but I think it creates harder to maintain apps when I have to pass around location strings that are irrelevant to the rest of the logic but are required by replaceState |
Wouldn't you do something like replaceState({query: {...query, ...next}, ...location}) ? |
@taion yes that looks nicer, but the caller still needs to know about the current query and location - I think maybe that shouldn't be necessary in all cases. Not that I want to generalize too many components, but just that those components don't necessarily need to know their location for any other reason, so I find it extraneous. |
|
@taion yup, that's how I get the location right now, but it just seems like this is a little extraneous to the core utility of the component; does replaceState know it's current state? |
If you don't specify things in full, how do we know whether you want to merge or replace the query? |
@taion You guys obviously have a vision for the API, but perhaps there would be a separate replaceQuery function for just that purpose. However, it is actually pretty common for the query object to already exist, since usually that is something which determines what to render. My initial instinct is: replaceState(
state: Object?
location: String? // if null, then use current location
query: Object? // if null, then use current query (this would replace the whole query if it exists)
) and replaceQuery(
mergeObject: Object // object to merge into current query
) Obviously adding a replaceQuery function is a huge, but just throwing it out there in case it catches on. |
I don't have a vision, I just want a good/usable/clear API that I can build my products on top of. I think something like a |
@taion agreed that it's a little messier, but the boilerplate is gettng a little overwhelming. I wonder if there are other alternatives, perhaps higher order functions to wrap replace state or a HOC that injects replaceState... Actually that might not be too bad. |
Certainly if you need a special use case, that might make sense. I don't think what I have above looks too bad with object spreads, though, and it's nice in being very explicit, which I think is a good property for a general-purpose API. |
Ahh, now I actually see the problem with relative URLs in
So for example doing something async and |
I'm a little confused by the idea of relative It should have to be the former, I think, but then that means that you wouldn't be able to reliably use relative links except on leaf route components, which is a bit weird. |
My confusion is this. Suppose your routes look like: <Route path="/foo" component={Parent}>
<Route path="bar" component={Child} />
</Route> Suppose you are on the path Where does this link take you? What if instead you're on the path I think the real challenge here is specifying what the correct behavior should be. |
The problem of relative URLs has been solved long time ago. Maybe I'm wrong but I think that we are just overthinking the problem here. And instead of thinking "what if" we should just follow the already specified standards. Have a look at RFC1808 |
Two problems:
|
We're bringing this to the router, work has started over here and will be brought into core when the kinks are worked out over there. |
This just bit me today. I gave a relative address to Link element without noticing and wondered what was wrong when it rendered the wildcard route. Curiously, it rendered the correct route when I used browser back button and then went forward. |
As part of this, we will want to put |
I am dropping this from the v3 milestone – while I very much think we should have this feature, I don't think we can treat this as a v3 blocker. |
I've read through this entire issue and I don't see what the problem is. Now I have to use something like https://www.npmjs.com/package/resolve-url and feed it |
I must say guys, even though I don't entirely agree with not following how a normal link works, I think amazing work is being done! |
So... is there gonna be a way to pass a relative link in the 'to' field or not? |
This is a feature that we want to support. I'm not aware that anybody is actively working on it, though. |
I'm sorry guys but after reading i do not understand what is already done and what is in plans. |
This is my "solution" :/ function splitURL(href) { let url = splitURL(window.location.href) + '/' + this.props.value; |
@txm look at solution by @optimatex, it is better because uses only |
Scratch my complaint. I was still under the impression that the resolution was workable for v2/v3 at some point. All in all does this mean that it won't be supported in the old v2/v3 branch ever but will only be available using the v4 Match syntax? |
I was under the assumption that v2/v3 was to be supported |
@saiichihashimoto v3 will receive bugfixes but no new features. Part of adding this particular feature meant that the code needed to be refactored in backwards-incompatible ways. |
Realize this is old, but is there a solution to this yet? Assume a path like: /deals/:dealId/trades/:tradeId/notes/:noteId. Further assume we're using tabs at each level. In order to link from one note to another, then, I need that component to know the context in which it exists. <Link to={`/deals/${dealId}/trades/${tradeId}/notes/${noteId}`}>Note 1 Tab</Link> and so forth. So a note needs to know its context when in fact it shouldn't. Is there an elegant pattern around this? |
See #5127 |
Right now it seems that the Link component is always absolute. It would be nice that if the
to
URL does not start with a/
that it becomes relative to the current route.E.g. current route is: http://localhost/#/zoo/
Clicking on the link should bring me to http://localhost/#/zoo/giraffe/ instead of http://localhost/#/giraffe/
The text was updated successfully, but these errors were encountered: