Replies: 7 comments 6 replies
-
I'm also very interested in what the "official solution" to this is. In my case, I am using a component system that has a Link component that just expects an There are other discussions like this one (but that only makes I also wrote my own solution based on copy/pasting and extending the types here, but this feels very ugly and (at least in my very brief test) blows up the TypeScript type check time by 2x. import { omit } from '@devoxa/flocky'
import {
Link as LinkFromPalette,
LinkDefaultProps as LinkFromPaletteDefaultProps,
LinkProps as LinkFromPaletteProps,
} from '@devoxa/palette'
import {
LinkPropsOptions,
MakeLinkOptions,
RegisteredRoutesInfo,
useLinkProps,
} from '@tanstack/router'
import React from 'react'
export type NextLinkProps = LinkPropsOptions & Omit<LinkFromPaletteProps, 'href' | 'navigate'>
export function Internal__NextLink(props: NextLinkProps) {
const linkProps = useLinkProps(props as LinkPropsOptions)
return (
<LinkFromPalette
{...omit(linkProps, ['color'])}
{...props}
// The props overwrite the "href" (it's defaulting to "#" because we use "to")
href={linkProps.href}
/>
)
}
Internal__NextLink.defaultProps = LinkFromPaletteDefaultProps
// Taken from the internal type definitions of @tanstack/react-router
interface LinkFn<
TDefaultFrom extends RegisteredRoutesInfo['routePaths'] = '/',
TDefaultTo extends string = '',
> {
<
TFrom extends RegisteredRoutesInfo['routePaths'] = TDefaultFrom,
TTo extends string = TDefaultTo,
>(
props: MakeLinkOptions<TFrom, TTo> &
React.RefAttributes<HTMLAnchorElement> &
NextLinkProps & { href?: never }
): JSX.Element
defaultProps: typeof LinkFromPaletteDefaultProps
}
export const NextLink = Internal__NextLink as LinkFn |
Beta Was this translation helpful? Give feedback.
-
My current solution for type ButtonLinkProps<TTo extends string = ''> = MakeLinkOptions<RoutePaths<RegisteredRouter['routeTree']>, TTo> & {
className?: string;
};
export const ButtonLink = <TTo extends string = ''>({ className, ...props }: ButtonLinkProps<TTo>) => (
<Link className={clsx(styles.button, className)} {...(props as any)} />
); |
Beta Was this translation helpful? Give feedback.
-
I have a different use-case that isn't solved by this workaround, as mentioned in #141 (comment) Basically, I want to do without the |
Beta Was this translation helpful? Give feedback.
-
Did you find a solution? I'm also interested in using custom component for links. I'm missing the |
Beta Was this translation helpful? Give feedback.
-
I found a solution how to type my custom Link component wrapper: https://stackoverflow.com/questions/77788907/how-to-wrap-a-link-component-with-type-safety I think writing a custom wrapper for the TanStack Link component is such a common scenario that there should be a documentation on how to do it (i.e. how to type it correctly) |
Beta Was this translation helpful? Give feedback.
-
Since creating a custom wrapper component for |
Beta Was this translation helpful? Give feedback.
-
If anyone lands on this, there's the relatively recent experimental For example, |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Basically I want to make a custom link component so that I can reuse throughout the app, so that I dont need to add
activeProps={{ className: 'text-blue-600' }}
forEach and every link that I have. And at the same time keep all the capabilities of the originalLink
component and be typesafe.Sorry if it was answered before I tried searching the discussions did not find any clear solution.
Beta Was this translation helpful? Give feedback.
All reactions