-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Search and path params in TanStack Router incompatible with useHref
#6587
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
Comments
I went ahead and created #6591 as I needed this change anyway to unblock myself at work. Let me know what you think. |
What do you think about making href optional for components if routerOptions is provided? For both Tanstack Router and React Router, routerOptions is solely needed in order to be able to generate the necessary href to attach to components. RouterProvider's useHref can then solely take in routerOptions instead of the href passed in to components. |
You can also define the declare module 'react-aria-components' {
interface RouterConfig {
href: YourRouterHref
}
} |
@devongovett Is there anything I can do to get a review on #6591? Maybe I missed something during the PR creation? |
Sorry just busy and we had the week off last week for a holiday. Will discuss with the team. |
@devongovett Thanks! Take your time. |
We discussed this today. We were originally thinking that An alternative suggestion would be to define the import {ToOptions, NavigateOptions} from '@tanstack/react-router';
declare module 'react-aria-components' {
interface RouterConfig {
href: ToOptions;
routerOptions: Omit<NavigateOptions, keyof ToOptions>;
}
} Then you can use them on a RAC component like this: <RACLink
href={{
to: '/dashboard/invoices/$invoiceId',
params: { invoiceId: 2 },
}} /> and get autocomplete for both This API also has the advantage that since both Here's a StackBlitz example. What do you think of this approach? |
@devongovett That is an interesting idea. Let me try that in my codebase. Currently I'm using the patch from my PR and wrap components to fix the auto-completion but this sounds much cleaner. |
@devongovett I gave it a try now but |
Yeah, that's what I meant. It should be possible, but I didn't quite figure it out. You'd basically need to generate a union of objects like I tried something like this: type Routes = NonNullable<ToOptions['to']>;
type Href = {
[To in Routes]: ToOptions<RegisteredRouter, string, To>
}[Routes]; but it didn't seem to work. I'm probably missing something. |
Succeeded in getting params to work with a small modification building on @devongovett above. Here's what I changed from the recommendation in the docs: import {type NavigateOptions, type ToOptions, useRouter} from '@tanstack/react-router';
import {RouterProvider} from 'react-aria-components';
declare module 'react-aria-components' {
interface RouterConfig {
- href: ToOptions['to'];
+ href: ToOptions;
routerOptions: Omit<NavigateOptions, keyof ToOptions>;
}
}
function RootRoute() {
let router = useRouter();
return (
<RouterProvider
- navigate={(to, options) => router.navigate({ to, ...options })}
+ navigate={(path, options) => router.navigate({ ...path, ...options })}
- useHref={(to) => router.buildLocation(to).href}
+ useHref={({ to }) => router.buildLocation({ to }).href}
>
{/* ... */}
</RouterProvider>
);
} This allows passing in params using an object for
|
@evadecker You should be able to pass the whole object passed to |
@levrik Changing it to |
@evadecker Passing as string doesn't work if you define just |
@levrik You're right. I could've sworn that |
Any status update on #6591 or other changes here? Unfortunately I'm still fighting with TanStack + RAC routing, and I'm a bit lost trying to dive into all the type definitions here between RAC's It'd be great if RAC's
|
Just adding that this continues to be a problem and it would be good to have a solution or even a workaround |
First of all, big thanks to everyone in this thread.
Passing entire <Link href={{ from: "/admin/forms", to: "$formId", params: { formId: form._id } }} /> The compiler complains that |
@mversic You have to type the full path in But I should also add that nailing down |
thanks, I figured that parameters in
oh, got it. Good that you pointed this out |
Yep as this requires properties to affect each other inside |
Provide a general summary of the issue here
TanStack Router interpolates path params into the href:
When using React Aria Components' router integration this gets turned into:
As
useHref
ofRouterProvider
only receives thehref
but notrouterOptions
likenavigate
does, the navigation itself works correctly but the native features like Cmd+Click (or right-click "Open in new tab") navigate to the href template instead of the real link. Also the URL preview shown by the browser is wrong.This could easily be solved by also passing
routerOptions
touseHref
.TanStack Router can also manage search params through a separate prop which suffers from the same issue.
I'm open to contributing this change but wanted to check upfront in case I'm missing something here. Maybe there is a reason for
routerOptions
not being passed touseHref
that I'm unaware of.🤔 Expected Behavior?
href
ona
tag to be/projects/123
with the above example.😯 Current Behavior
href
ona
tag is/projects/$id
with the above example.💁 Possible Solution
Pass
routerOptions
touseHref
as 2nd paramter.🔦 Context
No response
The text was updated successfully, but these errors were encountered: