-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Fix pathless route's match when parent is null #4704
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
Conversation
LGTM. What was the previous reasoning for not doing this? |
Change happened in f90d753. I think that it was because we can detect a pathless route inside of the This could still be done in the I'm not actually sure what the Edit: Also, apparently if you rebase and force update a commit that references an issue, GitHub doesn't forget 🤦♂️ |
It would seem to need to be recursive, going up the tree until it runs out of ancestors. That would mean the final ancestor would have the default path of |
Having I sort of went into a train of thought/rant about it in #4560, but even if we could access an ancestor { url: '/_FAKE_MATCH_DO_NOT_ATTEMPT_TO_RESOLVE_USING_THIS_OR_YOU_WILL_BE_DISAPPOINTED' } |
return parent != null | ||
? parent | ||
: { | ||
url: '/_FAKE_MATCH_DO_NOT_ATTEMPT_TO_RESOLVE_USING_THIS_OR_YOU_WILL_BE_DISAPPOINTED', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I actually did this. So far, the only use for match.url
that I have seen* has been with concatenating paths using the parent route. When the parent match is null
, we cannot provide an accurate parent match.url
. Having the error explicitly defined in the URL like this will make it more obvious to anyone who attempts to resolve using it that something is not right with what they are trying to do.
This isn't a make or break change for me, but I think that if we try to use some other value like root or the current URL, we will end up with a lot of people asking why they are getting the wrong URL when they attempt to resolve.
*It can actually be useful as a transition key for animated switches, but it isn't actually accessible with the <Switch>
component, so I'm ignoring that here.
I think that I might actually prefer leaving match The only real change that this would require is that The downside is that any user that uses a pathless <Route path='/some-place' children={() => (
// this would not work
<Route render={() => ...}/>
// this would
<Route children={() => ...}/>
)}/> When it comes to actually resolving paths, we could then reliably add a warning whenever someone attempts to resolve when the parent match is null. warning(
!(isRelative && parent == null),
'You are attempting to use a relative path when the parent match is null. This most likely ' +
'occurred because a parent <Route> uses the children prop and its path does not match ' +
'the current location\'s pathname'
) |
Just bumping this for consideration when you have the time @mjackson @ryanflorence. No rush, but it is holding up the relative routing PRs. |
I have some loose thoughts but nothing I can articulate yet, hopefully soon! |
So is there a way this situation can arise that is actually a sane situation? It seems to me that if you are using relative paths in a react tree that is not supposed to be matching, you are doing something wrong? In which case, relative Routes that get a |
I've faced an issue that my component Workaround that works for me:
|
@roboxv can you describe your tree up to your component? Indeed, withRouter is implemented with |
@wmertens is it okay if I just show you my files? From top to bottom: |
@roboxv That way, the children have a @pshrmn Is there anything blocking this issue from being merged? If so, what would be the best way for me to use your branch in my app in the meantime? |
@wmertens I don't think that there is anything blocking this. I'm pretty sure that @ryanflorence and @mjackson are just really busy right now. There isn't really a good way to install this branch. You could probably clone the repo from my fork, checkout the correct branch, and npm link it. Not the prettiest solution, but it should work. |
@ryanflorence is there anything I can do to convince you to click that [Merge] button? :) |
@ryanflorence should this PR be merged or not? |
@pshrmn the branch got stale again - maybe this time it will get merged! 😅 |
The logic for this has been moved to matchPath. When automatic path resolving is added, matchPath will need access to the parent match, so we will need to pass that argument to matchPath eventually anyways. When a parent match is null, a match object with default values is returned by matchPath. This match object should not be relied upon for resolving paths, but it will allow a <Route> that uses the component or render prop to render. Fixes remix-run#4695
This is only an issue when you use <Route children>. If you render a pathless <Route> inside of a <Route children>, you must also use <Route children> (instead of <Route render> or <Route component>). This commit also switches withRouter to using <Route children> instead of <Route render> so that it works when the parent match is null.
f0de291
to
aa1c228
Compare
Sorry for asking, but is there any progress in reviewing this? I'm running into an issue when using react-transition-group together with routes. When using a Route with children for the transition group (because the TransitionGroup always has to be mounted in order for the transition to work), components using An example demonstrating the issue is here: https://github.com/trygveaa/react-router-pathless-route-issue. A modal is shown when going to This PR fixes the issue. Any other suggestions on how to fix it within my code would also be appreciated though. |
I'm also having the issues described in #5141. |
fb3a586
to
f9481bb
Compare
Closing in favor of #5964 |
This moves the logic for getting the match object for a pathless
<Route>
matchPath
. When automatic path resolving is added,matchPath
will need access to the parentmatch
, so we will need to pass that argument tomatchPath
eventually anyways.When a parent
match
isnull
, a match object with default values is returned bymatchPath
. This match object should not be relied upon for resolving paths, but it will allow a<Route>
that uses thecomponent
orrender
prop to render.Fixes #4695