-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Support use of external links when using useHref
in RouterProvider
#6397
Comments
Could you wrap React Router's useHref in your own function and determine whether to return |
For anyone coming here looking for a copy-pastable solution function useAbsoluteHref(path: string) {
const relative = useHref(path);
if (
path.startsWith("https://") ||
path.startsWith("http://") ||
path.startsWith("mailto:")
) {
return path;
}
return relative;
} |
Not the prettiest, but I was able to extend // __root.tsx
import {
type NavigateOptions,
Outlet,
type ToOptions,
createRootRouteWithContext,
useRouter,
} from "@tanstack/react-router";
import { RouterProvider } from "react-aria-components";
declare module "react-aria-components" {
interface RouterConfig {
href: ToOptions | string;
routerOptions: Omit<NavigateOptions, keyof ToOptions>;
}
}
export const Route = createRootRoute()({
component: RootRoute,
});
function RootRoute() {
const router = useRouter();
return (
<RouterProvider
navigate={(path, options) =>
router.navigate(
typeof path === "string" ? { ...options } : { ...path, ...options },
)
}
useHref={(path) =>
typeof path === "string" ? path : router.buildLocation(path).href
}
>
<Outlet />
</RouterProvider>
);
}
In your component you'd use: // External links are strings
<Link href="https://github.com">GitHub</Link>
// Internal links are ToOptions objects
<Link href={{ to: '/about' }}>About</Link> This supports params as well: const myPostId = ...; // fetch param info
// Add params to your link
<Link href={{ to: '/post/$postId', { params: { postId: myPostId } }}>View Post</Link> |
My two cents for TanStack Router. declare module 'react-aria-components' {
interface RouterConfig {
href: ToOptions['to'] | (string & {})
routerOptions: Omit<NavigateOptions, keyof ToOptions>
}
}
<RouterProvider
navigate={(to, options) => router.navigate({ to, ...options })}
useHref={(to) => buildHref(to, router)}
>
<Outlet />
</RouterProvider>
const ABSOLUTE_URL_PREFIXES = ['https://', 'http://', 'mailto:'] as const
function buildHref(to: ToOptions['to'] | (string & {}), router: RegisteredRouter): string {
if (!to) return ''
if (ABSOLUTE_URL_PREFIXES.some((prefix) => to.startsWith(prefix))) {
return to
}
return router.buildLocation({ to }).href
} |
Provide a general summary of the feature here
The enhancements from #5864 have been great to support features we use from React Router. We are running into an issue though with the fact that when passing
useHref
toRouterProvider
we lose the ability to use external links as all RAChref
values get passed touseHref
. It would be convenient to have a way to opt-out ofuseHref
or detect an external link so both kinds can exist in an app.🤔 Expected Behavior?
Set
useHref
inRouterProvider
with React Router's version. There is a way to passhttps://www.google.com
to a RAChref
prop and opt-out of passing it touseHref
so that the final value ishttps://www.google.com
.😯 Current Behavior
Set
useHref
inRouterProvider
with React Router's version. Passinghttps://www.google.com
to a RAChref
prop results in[current_url]/https://www.google.com
.💁 Possible Solution
Add an optional prop for RAC where
href
androuterOptions
are available to opt-out of passing it throughuseHref
or detect an external link similar to how RR does this https://github.com/remix-run/react-router/blob/main/packages/react-router-dom/index.tsx#L958-L982🔦 Context
I want RAC links to support both client-side routing and external links simultaneously when using
useHref
inRouterProvider
.💻 Examples
No response
🧢 Your Company/Team
No response
🕷 Tracking Issue
No response
The text was updated successfully, but these errors were encountered: