Reuse type of <Link to="" #429
-
When building a component that wraps around the Link, e.g. a <NavItem, one would need the typings for the Tried a couple of things, but I think this snippet explains the intend well.
This however just gives me I also have the typings file:
Thoughts? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 8 replies
-
import { createRouteConfig } from '@tanstack/react-router'
import type { AllRouteInfo } from '@tanstack/react-router'
const routeConfig = createRouteConfig()
type RoutePaths = AllRouteInfo<typeof routeConfig>['routePaths'] type NavLinkProps = {
to: RoutePaths
}
function NavLink({ to }: NavLinkProps) {} |
Beta Was this translation helpful? Give feedback.
-
Use the newly exported MakeLinkOptions<> type and replicate the function signature for the normal Link component.
Tanner Linsley
…On Dec 7, 2022 at 5:28 PM -0700, moshyfawn ***@***.***>, wrote:
import { createRouteConfig } from ***@***.***/react-router'
import type { AllRouteInfo } from ***@***.***/react-router'
const routeConfig = createRouteConfig()
type RoutePaths = AllRouteInfo<typeof routeConfig>['routePaths']
type NavLinkProps = {
to: RoutePaths
}
function NavLink({ to }: NavLinkProps) {}
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
@Moshyfawn thanks! That does work 🎉 @tannerlinsley I am not quite sure if I am following, I tried using the MakeLinkOptions, but I am not quite sure what to pass where: Signature:
Doesn't seem to be quite right, do you have an example? Once you do, I happily put something into a sandbox as an example under guides or some if you think there is a fitting place for it. Maybe part of the Links guide? |
Beta Was this translation helpful? Give feedback.
-
for v0.83 version worked for me this, you can add you own properties:
|
Beta Was this translation helpful? Give feedback.
-
For now I use component type based on the LinkComponent: interface LinkFn<TElement extends ElementType> {
<
TFrom extends RoutePaths<RegisteredRouter["routeTree"]> = "/",
TTo extends string = "",
>(
props: ComponentProps<TElement> &
Omit<MakeLinkOptions<TFrom, TTo>, "activeProps"> & {
// Props that override the component's props when the link is active
// Why Omit? Use only custom props in the activeProps, not the anchor props.
activeProps?: Omit<Partial<ComponentProps<TElement>>, keyof AnchorHTMLAttributes<HTMLAnchorElement>>;
} & RefAttributes<HTMLAnchorElement>,
): ReactNode;
displayName?: string;
}
const RButton: LinkFn<typeof Button<"a">> = forwardRef(({ activeProps, children, ...rest }: any, ref) => {
const linkProps = useLinkProps(rest);
const isActive = (linkProps as any)?.["data-status"] === "active";
const props = isActive ? { ...linkProps, ...activeProps } : linkProps;
return (
<Button ref={ref} {...props} as={"a"}>
{children}
</Button>
);
});
RButton.displayName = "Router.Button"; https://stackblitz.com/edit/tanstack-router-dsnud7?file=src%2Fmain.tsx |
Beta Was this translation helpful? Give feedback.
-
found #1271 so will stop editing this now.. seems we now have
Edit.. I wanted to add more props for styling so went back and managed to update the "MakeLinkOptions" answer provided by Tanner Linsley above (#429 (reply in thread)) It does only appear to be link-specific props that are included though, so Also,
|
Beta Was this translation helpful? Give feedback.
-
Either one of these seems to work. Second one is less verbose at the function definition but has an import {
Link,
LinkProps,
RegisteredRouter,
RoutePaths as RP,
} from "@tanstack/react-router";
type RT = RegisteredRouter["routeTree"];
type MyLinkProps<
F extends RP<RT> | string,
T extends string,
MF extends RP<RT> | string,
MT extends string,
> = LinkProps<RT, F, T, MF, MT> &
React.PropsWithoutRef<Omit<React.HTMLProps<"a">, "children" | "preload">> &
React.RefAttributes<React.ComponentRef<"a">>;
export function LinkWithCustomProp<
F extends RP<RT> | string = string,
T extends string = "",
MF extends RP<RT> | string = F,
MT extends string = "",
>(props: MyLinkProps<F, T, MF, MT> & { myCustomProp: string }) {
const { myCustomProp, ...linkProps } = props;
return <Link {...(linkProps as MyLinkProps<F,T,MF,MT>)}>my custom prop is: {myCustomProp}</Link>;
}
type MyLinkComponent<CustomProps> = <
F extends RP<RT> | string = string,
T extends string = "",
MF extends RP<RT> | string = F,
MT extends string = "",
>(
props: MyLinkProps<F, T, MF, MT> & CustomProps,
) => React.ReactElement;
export const AlsoLinkWithCustomProp: MyLinkComponent<{ myCustomProp: string }> =
function AlsoLinkWithCustomProp(props) {
const { myCustomProp, ...linkProps } = props;
return (
<Link {...(linkProps as any)}>my custom prop is: {myCustomProp}</Link>
);
}; |
Beta Was this translation helpful? Give feedback.
@Moshyfawn thanks! That does work 🎉
@tannerlinsley I am not quite sure if I am following, I tried using the MakeLinkOptions, but I am not quite sure what to pass where:
Signature:
Doesn't seem to be quite right, do you have an example? Once you do, I happily put something into a sandbox as an example under guides or some if you think there is a fitting place for it.
Maybe part of the Links guide?