Skip to content

feat(react-router): add support for AbsoluteRoutes/useAbsoluteRoutes #13706

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

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silly-worms-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router": minor
---

Implement `<AbsoluteRoutes>`/`useAbsoluteRoutes` as alternatiuves to `<Routes>`/`useRoutes` when using absolute paths is required. This is primarily intended to be used to ease migrations from v5 applications where this was a common pattern. It's also useful for descendant routes when you want to manage your paths in an external data structure.
47 changes: 47 additions & 0 deletions docs/api/components/AbsoluteRoutes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: AbsoluteRoutes
---

# AbsoluteRoutes

[MODES: framework, data, declarative]

## Summary

[Reference Documentation ↗](https://api.reactrouter.com/v7/functions/react_router.AbsoluteRoutes.html)

An alternate version of [<Routes>](./Routes) that expects absolute paths on routes instead of relative paths. This is mostly intended to be used as a tool to help migrate from v5 where absolute paths were a common pattern, or for when you want to define your paths in a separate data structure using absolute paths.

```tsx
import { AbsoluteRoutes, Route } from "react-router";

<AbsoluteRoutes>
<Route path="/dashboard/*" element={<Dashboard />} />
</AbsoluteRoutes>;

function Dashboard() {
return (
<AbsoluteRoutes>
<Route
path="/dashboard/settings"
element={<Settings />}
/>
<Route path="/dashboard/users" element={<Users />} />
</AbsoluteRoutes>
);
}
```

## Props

### children

[modes: framework, data, declarative]

Nested [Route](../components/Route) elements

### location

[modes: framework, data, declarative]

The location to match against. Defaults to the current location.
78 changes: 78 additions & 0 deletions docs/api/hooks/useAbsoluteRoutes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
title: useAbsoluteRoutes
---

# useAbsoluteRoutes

[MODES: framework, data, declarative]

## Summary

[Reference Documentation ↗](https://api.reactrouter.com/v7/functions/react_router.useAbsoluteRoutes.html)

An alternate version of [useRoutes](./useRoutes) that expects absolute paths on routes instead of relative paths. This is mostly intended to be used as a tool to help migrate from v5 where absolute paths were a common pattern, or for when you want to define your paths in a separate data structure using absolute paths.

The return value of `useAbsoluteRoutes` is either a valid React element you can use to render the route tree, or `null` if nothing matched.

```tsx
import * as React from "react";
import { useAbsoluteRoutes } from "react-router";

const routes = {
dashboard: {
path: "/dashboard",
href: () => `/dashboard`,
},
dashboardMessages: {
path: "/dashboard/messages",
href: () => `/dashboard/messages`,
},
dashboardMessage: {
path: "/dashboard/:id",
href: (id: number) => `/dashboard/${id}`,
},
};

function App() {
let element = useAbsoluteRoutes([
{
path: routes.dashboard.path,
element: <Dashboard />,
children: [
{
path: routes.dashboardMessages.path,
element: <DashboardMessages />,
children: [
{
path: routes.dashboardMessage.path,
element: <DashboardMessage />,
},
],
},
],
},
]);

return element;
}
```

## Signature

```tsx
useAbsoluteRoutes(routes, locationArg): undefined
```

## Params

### routes

[modes: framework, data, declarative]

Your routes to use to render this location

### locationArg

[modes: framework, data, declarative]

The location to render instead of the current location
Loading