-
Notifications
You must be signed in to change notification settings - Fork 1.3k
docs: Update TanStack Router guide #8632
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -268,29 +268,17 @@ export default function App() { | |||||
} | ||||||
``` | ||||||
|
||||||
### TanStack Router | ||||||
## TanStack Router | ||||||
|
||||||
To use [TanStack Router](https://tanstack.com/router) with React Aria, render React Aria's `RouterProvider` inside your root route. Use `router.navigate` in the `navigate` prop, and `router.buildLocation` in the `useHref` prop. You can also configure TypeScript to get autocomplete for the `href` prop by declaring the `RouterConfig` type using the types provided by TanStack Router. | ||||||
To use [TanStack Router](https://tanstack.com/router) with React Aria, use the [createLink](https://tanstack.com/router/latest/docs/framework/react/guide/custom-link) function to wrap each React Aria component as a link. `RouterProvider` is not needed. | ||||||
|
||||||
```tsx | ||||||
import {useRouter, type NavigateOptions, type ToOptions} from '@tanstack/react-router'; | ||||||
import {RouterProvider} from 'react-aria-components'; | ||||||
// src/Link.tsx | ||||||
import {createLink} from '@tanstack/react-router'; | ||||||
import {Link as ReactAriaLink, MenuItem} from 'react-aria-components'; | ||||||
|
||||||
declare module 'react-aria-components' { | ||||||
interface RouterConfig { | ||||||
href: ToOptions; | ||||||
routerOptions: Omit<NavigateOptions, keyof ToOptions>; | ||||||
} | ||||||
} | ||||||
|
||||||
function RootRoute() { | ||||||
let router = useRouter(); | ||||||
return ( | ||||||
<RouterProvider | ||||||
navigate={(href, opts) => router.navigate({...href, ...opts})} | ||||||
useHref={href => router.buildLocation(href).href}> | ||||||
{/* ...*/} | ||||||
</RouterProvider> | ||||||
); | ||||||
} | ||||||
export const Link = createLink(ReactAriaLink); | ||||||
export const MenuItemLink = createLink(MenuItem); | ||||||
``` | ||||||
|
||||||
In you app, use these components instead of importing directly from `react-aria-components`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -270,32 +270,17 @@ export default function App() { | |||||
} | ||||||
``` | ||||||
|
||||||
### TanStack Router | ||||||
## TanStack Router | ||||||
|
||||||
To use [TanStack Router](https://tanstack.com/router) with React Spectrum, render React Spectrum's `Provider` inside your root route. Use `router.navigate` in the `navigate` prop, and `router.buildLocation` in the `useHref` prop. You can also configure TypeScript to get autocomplete for the `href` prop by declaring the `RouterConfig` type using the types provided by TanStack Router. | ||||||
To use [TanStack Router](https://tanstack.com/router) with React Spectrum, use the [createLink](https://tanstack.com/router/latest/docs/framework/react/guide/custom-link) function to wrap each React Spectrum component as a link. `RouterProvider` is not needed. | ||||||
|
||||||
```tsx | ||||||
import {useRouter, type NavigateOptions, type ToOptions} from '@tanstack/react-router'; | ||||||
import {Provider, defaultTheme} from '@adobe/react-spectrum'; | ||||||
|
||||||
declare module '@adobe/react-spectrum' { | ||||||
interface RouterConfig { | ||||||
href: ToOptions['to']; | ||||||
routerOptions: Omit<NavigateOptions, keyof ToOptions>; | ||||||
} | ||||||
} | ||||||
// src/Link.tsx | ||||||
import {createLink} from '@tanstack/react-router'; | ||||||
import {Link as SpectrumLink, Item} from '@adobe/react-spectrum'; | ||||||
|
||||||
function RootRoute() { | ||||||
let router = useRouter(); | ||||||
return ( | ||||||
<Provider | ||||||
theme={defaultTheme} | ||||||
router={{ | ||||||
navigate: (to, options) => router.navigate({to, ...options}), | ||||||
useHref: to => router.buildLocation({to}).href | ||||||
}}> | ||||||
{/* ...*/} | ||||||
</Provider> | ||||||
); | ||||||
} | ||||||
export const Link = createLink(SpectrumLink); | ||||||
export const ItemLink = createLink(Item); | ||||||
``` | ||||||
|
||||||
In you app, use these components instead of importing directly from `@adobe/react-spectrum`. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we wanna perhaps list the version (aka this latest release) that is needed for this to work? I know sometimes people get confused since they don't know when a new feature/fix got added/updated