Sub topic 1
What framework mode adds to React Router
Prerequisites
This chapter assumes you already know the following:
- React components, props, state, and hooks.
- TypeScript basics, including type imports.
- Node.js and npm basics.
- HTTP requests, responses, status codes, and cookies.
- HTML forms and the
FormDataAPI.
You do not need to have used React Router before. If you have, the first section places your existing knowledge in a larger picture.
One library, three modes
React Router v7 is a single package that you can use in three ways. Each mode adds features on top of the previous one.
| Mode | You write | The router provides |
|---|---|---|
| Declarative | `<BrowserRouter>` and `<Route>` elements | URL matching, `Link`, `Outlet`, navigation hooks |
| Data | A route object array passed to `createBrowserRouter` | Everything above, plus `loader`, `action`, and pending state |
| Framework | A route config file and route modules | Everything above, plus a Vite build, server rendering, generated types, and code splitting |
Declarative mode matches URLs to components. You fetch data yourself, usually with useEffect.
Data mode adds loader and action functions to routes. React Router calls them, tracks the navigation, and revalidates data after a mutation. Everything still runs in the browser.
Framework mode wraps data mode in a build tool and a server runtime. You still write loaders and actions, but they can run on the server. The framework also handles the parts around them: the build, the HTML document, and deployment.
What framework mode adds
Framework mode is a compiler plus a runtime. The @react-router/dev package integrates with Vite. It reads your configuration, builds your routes, and produces both a server bundle and a client bundle.
It adds these features:
- A route config file.
app/routes.tsmaps URL paths to route module files. - Route modules. A file can export a
loader,action,defaultcomponent,ErrorBoundary,meta, andlinks. React Router reads these exports by name. - Server rendering. The server can render the first page to HTML, so users see content before JavaScript loads.
- Generated types. The framework writes types for each route, so
loaderDataandparamsare typed without manual annotations. - Automatic code splitting. Each route module becomes its own chunk. The browser downloads only the routes it needs.
- Rendering options. In
react-router.config.ts, you choose server rendering, prerendering at build time, or a single-page app output.
A route module in one file
In framework mode, one file describes everything about a route: its data, its mutations, its UI, and its failure case. This example loads a contact on the server and passes it to the component.
import { data } from "react-router";
import type { Route } from "./+types/detail";
import { getContact } from "~/contacts.server";
export async function loader({ params }: Route.LoaderArgs) {
const contact = await getContact(params.contactId);
if (!contact) {
throw data("Contact not found", { status: 404 });
}
return { contact };
}
export default function ContactDetail({ loaderData }: Route.ComponentProps) {
return <h1>{loaderData.contact.name}</h1>;
}Three things are different from data mode:
- The
loaderis exported from the module. You do not attach it to a route object. - The
Routetypes come from./+types/detail. The framework generates this file from your route config. - The component receives
loaderDataas a prop, so you do not calluseLoaderData().
The ~/contacts.server import ends in .server. The build refuses to put that module in the client bundle. A later chapter covers this boundary.
How a request flows
When a browser requests a URL, the server does the following:
- It matches the URL against the route config.
- It runs the
loaderfor every matched route, in parallel. - It renders the matched components to HTML with the loader data.
- It sends the HTML to the browser.
- The browser loads the JavaScript and hydrates the page. Hydration attaches event handlers to the existing HTML.
After that, link clicks do not reload the page. The browser fetches only the data for the routes that changed, and React Router updates the page.
The same route modules run in both places. The loader runs on the server during the first request. The component runs on the server for the first render, then in the browser. This is why server-only code must live in files the client bundle never includes.
What stays the same
Framework mode is not a different router. Matching, nesting, Outlet, Link, Form, and useNavigation work as they do in data mode. If you learned them there, you keep that knowledge.
What changes is where the pieces live and who wires them together. In data mode you build the router object and mount it. In framework mode the framework generates the entry points and the server, and you supply files.
Choosing a mode
Pick the mode based on what you need, not on which is newest.
- Choose declarative mode when you add routing to an existing app and have no use for data APIs.
- Choose data mode when you want loaders and actions but own the build setup, for example in a custom bundler or an existing single-page app.
- Choose framework mode for a new app, especially when you want server rendering, typed routes, or a single toolchain.
The trade-off is control. Framework mode sets conventions for the build, entry files, and file layout. You can customize them, but you work inside the framework's structure. It also needs Vite, so an app tied to another bundler needs a migration.
Common mistakes
- Mixing APIs from different modes. For example,
<BrowserRouter>does not belong in a framework mode app, because the framework creates the router. - Importing server-only code into a component. If a component imports a database client directly, the client bundle tries to include it. Keep that code in
.servermodules and call it from loaders and actions. - Assuming loaders run in the browser. In framework mode, a
loaderruns on the server by default. UseclientLoaderwhen you need browser-side loading.
Summary of the plan
The rest of this tutorial builds one contacts app. You will create the project, configure routes, load data, handle forms, add error handling, protect routes with sessions, and deploy. Each chapter adds one capability to the same route modules.
Exercise
Your team has a working single-page React app built with Webpack. You want to add loaders and actions, but you cannot replace the bundler this quarter. Which React Router mode fits, and why?
Show answer
Data mode. It adds loaders, actions, and pending state to a browser-rendered router, and it does not require Vite. Framework mode needs the Vite-based toolchain, so adopting it would mean changing the bundler.
Exercise
In framework mode, a route module exports a loader and a default component. On the first page load, where does each run, and what happens afterward when the user clicks a link to another route?
Show answer
On the first load, the loader runs on the server, and the component runs on the server to produce HTML and again in the browser after hydration. When the user clicks a link, the browser requests the loader data for the routes that changed, and React Router renders the new components without a full page reload.