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)

Most upvoted comments

Just adding that this continues to be a problem and it would be good to have a solution or even a workaround

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’s RouterProvider, and TanStack, which overlap in ways that aren’t totally clear to me.

It’d be great if RAC’s Link component could handle these three cases out-of-the-box:

  • Accept an href as a string with Intellisense for existing routes (works today), e.g. <Link href="/about" />
  • Accept search and path params for dynamic routing with TanStack router (the changes suggested above enable this, but degrade DX by forcing engineers to provide an object to href instead of just a string), e.g. <Link href="/posts/$postId" params={{ postId: myPostId }} />
  • Accept an external link, e.g. <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’s href still throws an error:

CleanShot 2024-09-02 at 13 13 36@2x

We discussed this today. We were originally thinking that routerOptions was meant for things that affected the behavior of the navigation rather than only the URL itself (e.g. replace and resetScroll). We could add them to useHref but it would sort of mix concerns.

An alternative suggestion would be to define the href as an object corresponding to all of the options that TanStack router accepts rather than just a string.

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 to and params.

This API also has the advantage that since both to and params are 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 href type to accept an object instead of a string if you want.

declare module 'react-aria-components' {
  interface RouterConfig {
    href: YourRouterHref
  }
}