-
-
Notifications
You must be signed in to change notification settings - Fork 720
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
from
does not allow route-less paths
#3010
Comments
I don't understand the problem here. if the route does not exist, why should it be valid value? |
I have identical routes between two different parents
Nearly all routes under these function RouteComponent() {
return <Page from={Route.to} />;
} This works, but the I thought about making a hook to avoid passing down const adminFrom = '/Admin';
const enterpriseAdminFrom = '/enterprise/$enterpriseId/Admin';
export function useAdminFrom() {
const matchRoute = useMatchRoute();
if (isStaff) {
if (matchRoute({ to: enterpriseAdminFrom, fuzzy: true }) !== false) {
return enterpriseAdminFrom;
}
} else if (matchRoute({ to: adminFrom, fuzzy: true }) !== false) {
return adminFrom;
}
throw new Error('Do not use useAdminFrom outside admin routes');
} but because I could use absolute paths, but I'd have to handle both staff and user links everywhere: const enterpriseId = /* get id from route */
assert(isStaff && enterpriseId);
const link = isStaff
? linkOptions({ to: '/enterprise/$enterpriseId/Admin/path', params={{ enterpriseId }} })
: linkOptions({ to: '/Admin/path' }); I'm curious to know what the reasoning is behind not allowing |
can you please enhance your example to reflect your actual use case more closely? |
Here's an enhanced example: https://stackblitz.com/edit/github-9rvubqfj?file=src%2FAdmin%2Fusers%2Flist.tsx Try replacing |
Here is a possible solution: https://stackblitz.com/edit/github-9rvubqfj-yejwxqa4?file=src%2Fhooks%2FuseAdminFrom.ts,src%2FAdmin%2Fusers%2Flist.tsx
The restriction of the current implementation is that |
Is it safe if the |
please go in more detail which scenario you mean |
The exact scenario in your solution: const to = `${from}/users/$userId` as const;
return (
<Link to={to} params={{ userId: '123' }}> |
In this particular scenario, yes. If you are currently on a route that has |
<Link from={`${from}/...`} />
<Link to={`${from}/...`} /> These both work well, but I lose autocompletion when typing the path. I guess it's a technical limitation of TS. |
Which project does this relate to?
Router
Describe the bug
Setting a
from
for a parent path that does not have a route results in a type and runtime error.Your Example Website or App
https://stackblitz.com/edit/github-rkuq9rkx?file=src%2Froutes%2F__root.tsx
Steps to Reproduce the Bug or Issue
routes/very/deep/path.tsx
from
is not allowed by the typesExpected behavior
Arbitrary
from
paths should be allowed as long as they're valid.Screenshots or Videos
No response
Platform
Additional context
No response
The text was updated successfully, but these errors were encountered: