--- title: Custom Link --- While repeating yourself can be acceptable in many situations, you might find that you do it too often. At times, you may want to create cross-cutting components with additional behavior or styles. You might also consider using third-party libraries in combination with TanStack Router’s type safety. ## `createLink` for cross-cutting concerns `createLink` creates a custom `Link` component with the same type parameters as `Link`. This means you can create your own component which provides the same type safety and typescript performance as `Link`. ### Basic example If you want to create a basic custom link component, you can do so with the following: ```tsx import * as React from 'react' import { createLink, LinkComponent } from '@tanstack/react-router' interface BasicLinkProps extends React.AnchorHTMLAttributes { // Add any additional props you want to pass to the anchor element } const BasicLinkComponent = React.forwardRef( (props, ref) => { return ( ) }, ) const CreatedLinkComponent = createLink(BasicLinkComponent) export const CustomLink: LinkComponent = (props) => { return } ``` You can then use your newly created `Link` component as any other `Link` ```tsx ``` ## `createLink` with third party libraries ### React Aria Components example React Aria Components’ [Link](https://react-spectrum.adobe.com/react-aria/Link.html) component does not support the standard `onMouseEnter` and `onMouseLeave` events. Therefore, you cannot use it directly with TanStack Router’s `preload (intent)` prop. Explanation for this can be found here: - [https://react-spectrum.adobe.com/react-aria/interactions.html](https://react-spectrum.adobe.com/react-aria/interactions.html) - [https://react-spectrum.adobe.com/blog/building-a-button-part-2.html](https://react-spectrum.adobe.com/blog/building-a-button-part-2.html) It is possible to work around this by using the [useLink](https://react-spectrum.adobe.com/react-aria/useLink.html) hook from [React Aria Hooks](https://react-spectrum.adobe.com/react-aria/hooks.html) with a standard anchor element. ```tsx 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 { children?: React.ReactNode } const RACLinkComponent = React.forwardRef( (props, forwardedRef) => { const ref = useObjectRef(forwardedRef) const { isPressed, linkProps } = useLink(props, ref) const { isHovered, hoverProps } = useHover(props) const { isFocusVisible, isFocused, focusProps } = useFocusRing(props) return ( ) }, ) const CreatedLinkComponent = createLink(RACLinkComponent) export const CustomLink: LinkComponent = (props) => { return } ``` ### Chakra UI example ```tsx import * as React from 'react' import { createLink, LinkComponent } from '@tanstack/react-router' import { Link } from '@chakra-ui/react' interface ChakraLinkProps extends Omit, 'href'> { // Add any additional props you want to pass to the link } const ChakraLinkComponent = React.forwardRef< HTMLAnchorElement, ChakraLinkProps >((props, ref) => { return }) const CreatedLinkComponent = createLink(ChakraLinkComponent) export const CustomLink: LinkComponent = ( props, ) => { return ( ) } ``` ### MUI example ```tsx import * as React from 'react' import { createLink, LinkComponent } from '@tanstack/react-router' import { Button, ButtonProps } from '@mui/material' interface MUILinkProps extends Omit { // Add any additional props you want to pass to the button } const MUILinkComponent = React.forwardRef( (props, ref) => { return