Skip to content

Commit abad0a1

Browse files
docs(react-router): update custom-link guide for react-aria (#4757)
As of [React Aria Components v1.11.0](https://react-spectrum.adobe.com/releases/2025-07-22.html), we now pass through DOM events, so it's no longer necessary to use hooks to create a custom link component. You can just use `createLink` with the React Aria component directly. [Demo](https://stackblitz.com/edit/stackblitz-starters-qc66mmzn?file=src%2FLink.tsx) --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent a89408a commit abad0a1

File tree

1 file changed

+24
-45
lines changed

1 file changed

+24
-45
lines changed

docs/router/framework/react/guide/custom-link.md

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -53,59 +53,38 @@ Here are some examples of how you can use `createLink` with third-party librarie
5353

5454
### React Aria Components example
5555

56-
React Aria Components'
57-
[Link](https://react-spectrum.adobe.com/react-aria/Link.html) component does not support the standard `onMouseEnter` and `onMouseLeave` events.
58-
Therefore, you cannot use it directly with TanStack Router's `preload (intent)` prop.
59-
60-
Explanation for this can be found here:
61-
62-
- [https://react-spectrum.adobe.com/react-aria/interactions.html](https://react-spectrum.adobe.com/react-aria/interactions.html)
63-
- [https://react-spectrum.adobe.com/blog/building-a-button-part-2.html](https://react-spectrum.adobe.com/blog/building-a-button-part-2.html)
64-
65-
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.
56+
React Aria Components v1.11.0 and later works with TanStack Router's `preload (intent)` prop. Use `createLink` to wrap each React Aria component that you use as a link.
6657

6758
```tsx
68-
import * as React from 'react'
69-
import { createLink, LinkComponent } from '@tanstack/react-router'
70-
import {
71-
mergeProps,
72-
useFocusRing,
73-
useHover,
74-
useLink,
75-
useObjectRef,
76-
} from 'react-aria'
77-
import type { AriaLinkOptions } from 'react-aria'
78-
79-
interface RACLinkProps extends Omit<AriaLinkOptions, 'href'> {
80-
children?: React.ReactNode
81-
}
59+
import { createLink } from '@tanstack/react-router'
60+
import { Link as RACLink, MenuItem } from 'react-aria-components'
8261

83-
const RACLinkComponent = React.forwardRef<HTMLAnchorElement, RACLinkProps>(
84-
(props, forwardedRef) => {
85-
const ref = useObjectRef(forwardedRef)
62+
export const Link = createLink(RACLink)
63+
export const MenuItemLink = createLink(MenuItem)
64+
```
8665

87-
const { isPressed, linkProps } = useLink(props, ref)
88-
const { isHovered, hoverProps } = useHover(props)
89-
const { isFocusVisible, isFocused, focusProps } = useFocusRing(props)
66+
To use React Aria's render props, including the `className`, `style`, and `children` functions, create a wrapper component and pass that to `createLink`.
9067

91-
return (
92-
<a
93-
{...mergeProps(linkProps, hoverProps, focusProps, props)}
94-
ref={ref}
95-
data-hovered={isHovered || undefined}
96-
data-pressed={isPressed || undefined}
97-
data-focus-visible={isFocusVisible || undefined}
98-
data-focused={isFocused || undefined}
99-
/>
100-
)
101-
},
102-
)
68+
```tsx
69+
import { createLink } from '@tanstack/react-router'
70+
import { Link as RACLink, type LinkProps } from 'react-aria-components'
10371

104-
const CreatedLinkComponent = createLink(RACLinkComponent)
72+
interface MyLinkProps extends LinkProps {
73+
// your props
74+
}
10575

106-
export const CustomLink: LinkComponent<typeof RACLinkComponent> = (props) => {
107-
return <CreatedLinkComponent preload={'intent'} {...props} />
76+
function MyLink(props: MyLinkProps) {
77+
return (
78+
<RACLink
79+
{...props}
80+
style={({ isHovered }) => ({
81+
color: isHovered ? 'red' : 'blue',
82+
})}
83+
/>
84+
)
10885
}
86+
87+
export const Link = createLink(MyLink)
10988
```
11089

11190
### Chakra UI example

0 commit comments

Comments
 (0)