Skip to content

Commit c671b89

Browse files
committed
more docs on the modes
1 parent 9d0020a commit c671b89

File tree

12 files changed

+207
-197
lines changed

12 files changed

+207
-197
lines changed

docs/index.md

Lines changed: 14 additions & 187 deletions
Original file line numberDiff line numberDiff line change
@@ -7,206 +7,33 @@ order: 1
77

88
React Router is a multi-strategy router for React bridging the gap from React 18 to React 19. You can use it maximally as a React framework or as minimally as you want.
99

10-
The router at the top of your app determines which features are available to the rest of the APIs. Each mode builds upon the previous to let you use as much or as little as you like.
10+
## Getting Started
1111

12-
- **Declarative**: minimalist usage, just URL matching, active states, and navigating. Classic and clean. If you're using `<BrowserRouter>`, you're using declarative routing.
13-
- **Data**: Everything from declarative but adds data features like loaders, actions, and pending states. If you're using `createBrowserRouter`, you're using data.
14-
- **Framework**: Let React Router do it all with efficient bundling, code splitting, server rendering, and advanced type safety. If you're using `routes.ts`, you're using the framework.
12+
There are three primary ways, or "modes", to use it in your app, so there are three guides to get you started.
1513

16-
Each API doc will list which modes in which it's available with this:
14+
- [Declarative](./start/declarative/installation)
15+
- [Data](./start/data/custom)
16+
- [Framework](./start/framework/installation)
1717

18-
[MODES: framework, data, declarative]
19-
20-
## Declarative
18+
Learn which mode is right for you in [Picking a Mode](./start/picking-a-router).
2119

22-
[modes: declarative]
20+
## Using These Guides
2321

24-
[Get Started](./start/library/installation) with Declarative routing.
22+
Across the docs you'll see the following icons:
2523

26-
Like previous versions, React Router can still be used as a simple, declarative routing library. Its only job will be matching the URL to a set of components, providing access to URL data, and navigating around the app.
24+
[MODES: framework, data, declarative]
2725

28-
This strategy is popular for "Single Page Apps" that have their own frontend infrastructure and v6 apps looking for a stress free upgrade.
26+
<p></p>
2927

30-
It's particularly good at offline + sync architectures where pending states are rare and users have long running sessions. Framework features like pending states, code splitting, server rendering, SEO, and initial page load times can be traded out for instant local-first interactions.
28+
These icons indicate which mode the content is relevant to.
3129

32-
```tsx
33-
ReactDOM.createRoot(root).render(
34-
<BrowserRouter>
35-
<Routes>
36-
<Route path="/" element={<Home />} />
37-
<Route path="dashboard" element={<Dashboard />}>
38-
<Route index element={<RecentActivity />} />
39-
<Route path="project/:id" element={<Project />} />
40-
</Route>
41-
</Routes>
42-
</BrowserRouter>
43-
);
44-
```
45-
46-
## Data
47-
48-
[modes: data]
49-
50-
[Get Started](./start/framework/custom) building a custom framework with a data router.
51-
52-
The framework features are built on top of lower-level APIs in React Router. You can use these APIs directly for a lighter-weight usage of React Router, but you'll need to set up your own bundling and server rendering (if you want it).
30+
Additional auto-generated reference documentation is available:
5331

54-
Some of the features include:
55-
56-
- Data loading with route loaders
57-
- Data mutations with actions
58-
- Concurrent mutations with fetchers
59-
- Race condition handling
60-
- Utilities to manage pending states
61-
62-
Routes and loaders are configured at runtime with `createBrowserRouter`.
63-
64-
```tsx
65-
import { createBrowserRouter } from "react-router";
66-
67-
let router = createBrowserRouter([
68-
{
69-
path: "/",
70-
Component: Root,
71-
children: [
72-
{
73-
path: "shows/:showId",
74-
Component: Show,
75-
loader: ({ request, params }) =>
76-
fetch(`/api/show/${params.id}.json`, {
77-
signal: request.signal,
78-
}),
79-
},
80-
],
81-
},
82-
]);
83-
```
84-
85-
The router is then rendered with `<RouterProvider>`:
86-
87-
```tsx
88-
import {
89-
createBrowserRouter,
90-
RouterProvider,
91-
} from "react-router";
92-
import { createRoot } from "react-dom/client";
93-
94-
createRoot(document.getElementById("root")).render(
95-
<RouterProvider router={router} />
96-
);
97-
```
98-
99-
Using a data router, you now have access to nearly every runtime API in React Router.
100-
101-
## Framework
102-
103-
[modes: framework]
104-
105-
[Get Started](./start/framework/installation) with React Router as a framework.
106-
107-
Building on top of the data mode, React Router can be used maximally as your React framework. In this setup, you'll use the React Router CLI and Vite bundler plugin for a full-stack development and deployment architecture. This enables React Router to provide a large set of features most web projects will want, including:
108-
109-
- Vite bundler and dev server integration
110-
- hot module replacement
111-
- code splitting
112-
- route conventions with type safety
113-
- file system or config-based routing
114-
- data loading with type safety
115-
- actions with type safety
116-
- automatic revalidation of page data after actions
117-
- SSR, SPA, and static rendering strategies
118-
- APIs for pending states and optimistic UI
119-
- deployment adapters
120-
121-
Routes are configured with `routes.ts` which enables React Router to do a lot for you. For example, it will automatically code-split each route, provide type safety for the parameters and data, and automatically load the data with access to pending states as the user navigates to it.
122-
123-
```ts
124-
import {
125-
type RouteConfig,
126-
route,
127-
index,
128-
layout,
129-
prefix,
130-
} from "@react-router/dev/routes";
131-
132-
export default [
133-
index("./home.tsx"),
134-
route("about", "./about.tsx"),
135-
136-
layout("./auth/layout.tsx", [
137-
route("login", "./auth/login.tsx"),
138-
route("register", "./auth/register.tsx"),
139-
]),
140-
141-
...prefix("concerts", [
142-
index("./concerts/home.tsx"),
143-
route(":city", "./concerts/city.tsx"),
144-
route(":city/:id", "./concerts/show.tsx"),
145-
route("trending", "./concerts/trending.tsx"),
146-
]),
147-
] satisfies RouteConfig;
148-
```
149-
150-
You'll have access to the Route Module API, which most of the other features are built on.
151-
152-
Loaders provide data to route components:
153-
154-
```tsx
155-
// loaders provide data to components
156-
export async function loader({ params }: Route.LoaderArgs) {
157-
const [show, isLiked] = await Promise.all([
158-
fakeDb.find("show", params.id),
159-
fakeIsLiked(params.city),
160-
]);
161-
return { show, isLiked };
162-
}
163-
```
164-
165-
Components render at their configured URLs from routes.ts with the loader data passed in as a prop:
166-
167-
```tsx
168-
export default function Show({
169-
loaderData,
170-
}: Route.ComponentProps) {
171-
const { show, isLiked } = loaderData;
172-
return (
173-
<div>
174-
<h1>{show.name}</h1>
175-
<p>{show.description}</p>
176-
177-
<form method="post">
178-
<button
179-
type="submit"
180-
name="liked"
181-
value={isLiked ? 0 : 1}
182-
>
183-
{isLiked ? "Remove" : "Save"}
184-
</button>
185-
</form>
186-
</div>
187-
);
188-
}
189-
```
190-
191-
Actions can update data and trigger a revalidation of all data on
192-
the page so your UI stays up to date automatically:
193-
194-
```tsx
195-
export async function action({
196-
request,
197-
params,
198-
}: Route.LoaderArgs) {
199-
const formData = await request.formData();
200-
await fakeSetLikedShow(formData.get("liked"));
201-
return { ok: true };
202-
}
203-
```
204-
205-
Route modules also provide conventions for SEO, asset loading, error boundaries, and more.
32+
[Autogenerated Reference Docs ↗](https://api.reactrouter.com/v7/modules/react_router.html)
20633

20734
## Upgrading
20835

209-
If you are caught up on future flags, upgrading from React Router v6 or Remix is generally non-breaking:
36+
If you are caught up on future flags, upgrading from React Router v6 or Remix v2 is generally non-breaking. Remix v2 apps are encouraged to upgrade to React Router v7.
21037

21138
- [Upgrade from v6](./upgrading/v6)
21239
- [Upgrade from Remix](./upgrading/remix)
File renamed without changes.

docs/start/data/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
title: Data Mode
3+
order: 3
4+
---

docs/start/declarative/index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
title: Declarative Mode
3+
order: 4
4+
---
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

docs/start/framework/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
---
2-
title: Framework
3-
order: 1
2+
title: Framework Mode
3+
order: 2
44
---

docs/start/library/data-routers.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)