-
Notifications
You must be signed in to change notification settings - Fork 1.2k
The instructions for client side routing for Tantsack Router doesn't work. #6413
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 tried to create a Stackblitz for this but uses an older version of typescript and I can't exactly reproduce it: Had to remove the |
Here's an example that is passing the params using the |
However, an "official" way to handle this would be much appreciated, so I'm bumping this .. 🙏 |
I played around with the suggested solution by @MAST1999. I needed to do some changes but it's starting to work. The bigger issue I'm seeing here is that it requires making everything that accepts |
Looks like the docs would need to be updated. This used to work but there have been some updates to TanStack Router's types that broke it. This should work instead: import {ToOptions, NavigateOptions} from '@tanstack/react-router';
declare module 'react-aria-components' {
interface RouterConfig {
href: ToOptions['to'];
routerOptions: Omit<NavigateOptions, keyof ToOptions>;
}
} You could also consider typing |
Update: I got it to work by The examples differ between the Spectrum and TanStack docs, but neither works exactly right: Spectrum: Extend RouterConfig to use TanStack navigation import {type NavigateOptions, type ToOptions, useRouter} from '@tanstack/react-router';
import {defaultTheme, Provider} from '@adobe/react-spectrum';
declare module '@adobe/react-spectrum' {
interface RouterConfig {
href: ToOptions['to'];
routerOptions: Omit<NavigateOptions, keyof ToOptions>;
}
}
function RootRoute() {
let router = useRouter();
return (
<Provider
theme={defaultTheme}
router={{
navigate: (to, options) => router.navigate({ to, ...options }),
useHref: (to) => router.buildLocation({ to }).href
}}
>
{/* ...*/}
</Provider>
);
} Problem with above approach: We lose out on the TanStack goodies such as TanStack: Use createLink to extend with RAC hooks. import * as React from 'react'
import { createLink, LinkComponent } from '@tanstack/react-router'
import {
mergeProps,
useFocusRing,
useHover,
useLink,
useObjectRef,
} from 'react-aria'
import type { AriaLinkOptions } from 'react-aria'
interface RACLinkProps extends Omit<AriaLinkOptions, 'href'> {
children?: React.ReactNode
}
const RACLinkComponent = React.forwardRef<HTMLAnchorElement, RACLinkProps>(
(props, forwardedRef) => {
const ref = useObjectRef(forwardedRef)
const { isPressed, linkProps } = useLink(props, ref)
const { isHovered, hoverProps } = useHover(props)
const { isFocusVisible, isFocused, focusProps } = useFocusRing(props)
return (
<a
{...mergeProps(linkProps, hoverProps, focusProps, props)}
ref={ref}
data-hovered={isHovered || undefined}
data-pressed={isPressed || undefined}
data-focus-visible={isFocusVisible || undefined}
data-focused={isFocused || undefined}
/>
)
},
)
const CreatedLinkComponent = createLink(RACLinkComponent)
export const CustomLink: LinkComponent<typeof RACLinkComponent> = (props) => {
return <CreatedLinkComponent preload={'intent'} {...props} />
} Problem with above approach: AFAIK, The reason I say doesn't fully extend is because I tried the custom link approach and extended both So.... any ideas? |
🙋 Documentation Request
Currently, if you implement it as the documentation has explained, you get this error:

To get rid of the error, you can change it to this:
But this makes the
href
becomes juststring
, and you get no auto complete.The way to get autocomplete is doing this:
And change the router provider to this:
Now you will get auto complete in the links, and you can use any string as well:

This would also enable using the

params
andsearch
fromtanstack/react-router
to be typesafe:But right now since there is no way to constrict them based on the given
href
they show all the options for all the routes.I think the way to fix this would be to give
RouterConfig
a generic string parameter and pass it toToPathOption
but I'm not sure.I'm not too familiar with how the types are set up for React Aria, but what I'm hoping for is this will link the
href
value toToPathOption
andNavigateOptions
and show the correct values forsearch
andparams
options.🧢 Your Company/Team
No response
The text was updated successfully, but these errors were encountered: