UNPKG

9.02 kB Markdown View Raw
1---
2title: Picking a Mode
3order: 1
4---
5
6# Picking a Mode
7
8React Router is a multi-strategy router for React. There are three primary ways, or "modes", to use it in your app. Across the docs you'll see these icons indicating which mode the content is relevant to:
9
10[MODES: framework, data, declarative]
11
12<p></p>
13
14The features available in each mode are additive, so moving from Declarative to Data to Framework simply adds more features at the cost of architectural control. So pick your mode based on how much control or how much help you want from React Router.
15
16The mode depends on which "top level" router API you're using:
17
18## Declarative
19
20Declarative mode enables basic routing features like matching URLs to components, navigating around the app, and providing active states with APIs like `<Link>`, `useNavigate`, and `useLocation`.
21
22```tsx
23import { BrowserRouter } from "react-router";
24
25ReactDOM.createRoot(root).render(
26 <BrowserRouter>
27 <App />
28 </BrowserRouter>,
29);
30```
31
32## Data
33
34By moving route configuration outside of React rendering, Data Mode adds data loading, actions, pending states and more with APIs like `loader`, `action`, and `useFetcher`.
35
36```tsx
37import {
38 createBrowserRouter,
39 RouterProvider,
40} from "react-router";
41
42let router = createBrowserRouter([
43 {
44 path: "/",
45 Component: Root,
46 loader: loadRootData,
47 },
48]);
49
50ReactDOM.createRoot(root).render(
51 <RouterProvider router={router} />,
52);
53```
54
55## Framework
56
57Framework Mode wraps Data Mode with a Vite plugin to add the full React Router experience with:
58
59- type-safe `href`
60- type-safe Route Module API
61- intelligent code splitting
62- SPA, SSR, and static rendering strategies
63- and more
64
65```ts filename=routes.ts
66import { index, route } from "@react-router/dev/routes";
67
68export default [
69 index("./home.tsx"),
70 route("products/:pid", "./product.tsx"),
71];
72```
73
74You'll then have access to the Route Module API with type-safe params, loaderData, code splitting, SPA/SSR/SSG strategies, and more.
75
76```ts filename=product.tsx
77import { Route } from "./+types/product.tsx";
78
79export async function loader({ params }: Route.LoaderArgs) {
80 let product = await getProduct(params.pid);
81 return { product };
82}
83
84export default function Product({
85 loaderData,
86}: Route.ComponentProps) {
87 return <div>{loaderData.product.name}</div>;
88}
89```
90
91## Decision Advice
92
93Every mode supports any architecture and deployment target, so the question isn't really about if you want SSR, SPA, etc. It's about how much you want to do yourself.
94
95**Use Framework Mode if you:**
96
97- are too new to have an opinion
98- are considering Next.js, Solid Start, SvelteKit, Astro, TanStack Start, etc. and want to compare
99- just want to build something with React
100- might want to server render, might not
101- are coming from Remix (React Router v7 is the "next version" after Remix v2)
102- are migrating from Next.js
103
104[→ Get Started with Framework Mode](./framework/installation).
105
106**Use Data Mode if you:**
107
108- want data features but also want to have control over bundling, data, and server abstractions
109- started a data router in v6.4 and are happy with it
110
111[→ Get Started with Data Mode](./data/custom).
112
113**Use Declarative Mode if you:**
114
115- want to use React Router as simply as possible
116- are coming from v6 and are happy with the `<BrowserRouter>`
117- have a data layer that either skips pending states (like local first, background data replication/sync) or has its own abstractions for them
118- are coming from Create React App (you may want to consider framework mode though)
119
120[→ Get Started with Declarative Mode](./declarative/installation).
121
122## API + Mode Availability Table
123
124This is mostly for the LLMs, but knock yourself out:
125
126| API | Framework | Data | Declarative |
127| ------------------------------ | --------- | ---- | ----------- |
128| Await | ✅ | ✅ | |
129| Form | ✅ | ✅ |
130| Link | ✅ | ✅ | ✅ |
131| `<Link discover>` | ✅ | | |
132| `<Link prefetch>` | ✅ | | |
133| `<Link preventScrollReset>` | ✅ | ✅ | |
134| Links | ✅ | | |
135| Meta | ✅ | | |
136| NavLink | ✅ | ✅ | ✅ |
137| `<NavLink discover>` | ✅ | | |
138| `<NavLink prefetch>` | ✅ | | |
139| `<NavLink preventScrollReset>` | ✅ | ✅ | |
140| NavLink `isPending` | ✅ | ✅ | |
141| Navigate | ✅ | ✅ | ✅ |
142| Outlet | ✅ | ✅ | ✅ |
143| PrefetchPageLinks | ✅ | | |
144| Route | ✅ | ✅ | ✅ |
145| Routes | ✅ | ✅ | ✅ |
146| Scripts | ✅ | | |
147| ScrollRestoration | ✅ | ✅ | |
148| ServerRouter | ✅ | | |
149| usePrompt | ✅ | ✅ | |
150| useActionData | ✅ | ✅ | |
151| useAsyncError | ✅ | ✅ | |
152| useAsyncValue | ✅ | ✅ | |
153| useBeforeUnload | ✅ | ✅ | ✅ |
154| useBlocker | ✅ | ✅ | |
155| useFetcher | ✅ | ✅ | |
156| useFetchers | ✅ | ✅ | |
157| useFormAction | ✅ | ✅ | |
158| useHref | ✅ | ✅ | ✅ |
159| useInRouterContext | ✅ | ✅ | ✅ |
160| useLinkClickHandler | ✅ | ✅ | ✅ |
161| useLoaderData | ✅ | ✅ | |
162| useLocation | ✅ | ✅ | ✅ |
163| useMatch | ✅ | ✅ | ✅ |
164| useMatches | ✅ | ✅ | |
165| useNavigate | ✅ | ✅ | ✅ |
166| useNavigation | ✅ | ✅ | |
167| useNavigationType | ✅ | ✅ | ✅ |
168| useOutlet | ✅ | ✅ | ✅ |
169| useOutletContext | ✅ | ✅ | ✅ |
170| useParams | ✅ | ✅ | ✅ |
171| useResolvedPath | ✅ | ✅ | ✅ |
172| useRevalidator | ✅ | ✅ | |
173| useRouteError | ✅ | ✅ | |
174| useRouteLoaderData | ✅ | ✅ | |
175| useRoutes | ✅ | ✅ | ✅ |
176| useSearchParams | ✅ | ✅ | ✅ |
177| useSubmit | ✅ | ✅ | |
178| useViewTransitionState | ✅ | ✅ | |
179| isCookieFunction | ✅ | ✅ | |
180| isSessionFunction | ✅ | ✅ | |
181| createCookie | ✅ | ✅ | |
182| createCookieSessionStorage | ✅ | ✅ | |
183| createMemorySessionStorage | ✅ | ✅ | |
184| createPath | ✅ | ✅ | ✅ |
185| createRoutesFromElements | | ✅ | |
186| createRoutesStub | ✅ | ✅ | |
187| createSearchParams | ✅ | ✅ | ✅ |
188| data | ✅ | ✅ | |
189| generatePath | ✅ | ✅ | ✅ |
190| href | ✅ | | |
191| isCookie | ✅ | ✅ | |
192| isRouteErrorResponse | ✅ | ✅ | |
193| isSession | ✅ | ✅ | |
194| matchPath | ✅ | ✅ | ✅ |
195| matchRoutes | ✅ | ✅ | ✅ |
196| parsePath | ✅ | ✅ | ✅ |
197| redirect | ✅ | ✅ | |
198| redirectDocument | ✅ | ✅ | |
199| renderMatches | ✅ | ✅ | ✅ |
200| replace | ✅ | ✅ | |
201| resolvePath | ✅ | ✅ | ✅ |