react-spectrum: Search and path params in TanStack Router incompatible with `useHref`
Provide a general summary of the issue here
TanStack Router interpolates path params into the href:
<Link to="/projects/$id" params={{ id: '123' }}>Open</Link>
When using React Aria Components’ router integration this gets turned into:
<Link href="/projects/$id" routerOptions={{ params: { id: '123' } }}>Open</Link>
As useHref of RouterProvider only receives the href but not routerOptions like navigate 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 to useHref.
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 to useHref that I’m unaware of.
🤔 Expected Behavior?
href on a tag to be /projects/123 with the above example.
😯 Current Behavior
href on a tag is /projects/$id with the above example.
💁 Possible Solution
Pass routerOptions to useHref as 2nd paramter.
🔦 Context
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 3
- Comments: 23 (10 by maintainers)
Just adding that this continues to be a problem and it would be good to have a solution or even a workaround
Work done here https://github.com/adobe/react-spectrum/pull/8218
Any status update on https://github.com/adobe/react-spectrum/pull/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
Link, RAC’sRouterProvider, and TanStack, which overlap in ways that aren’t totally clear to me.It’d be great if RAC’s
Linkcomponent could handle these three cases out-of-the-box:hrefas a string with Intellisense for existing routes (works today), e.g.<Link href="/about" />hrefinstead of just a string), e.g.<Link href="/posts/$postId" params={{ postId: myPostId }} /><Link href="https://github.com" />@levrik Changing it to
useHref={(path) => router.buildLocation(path).href}works the same as destructuring{ to }. But passing a string as a component’shrefstill throws an error:We discussed this today. We were originally thinking that
routerOptionswas meant for things that affected the behavior of the navigation rather than only the URL itself (e.g.replaceandresetScroll). We could add them touseHrefbut it would sort of mix concerns.An alternative suggestion would be to define the
hrefas an object corresponding to all of the options that TanStack router accepts rather than just a string.Then you can use them on a RAC component like this:
and get autocomplete for both
toandparams.This API also has the advantage that since both
toandparamsare part of the same type, it’s possible for typescript to infer the params based on the route. This would require a bit more work with TanStack Router’s types to get working, but it should be possible. Also good news is that this should be possible today without any code changes.Here’s a StackBlitz example.
What do you think of this approach?
You can also define the
hreftype to accept an object instead of a string if you want.