Sub topic 1
What framework mode adds to React Router
Prerequisites
This chapter assumes you know the following:
- React components, props, state, and hooks
- TypeScript basics
- Node.js and npm basics
- HTTP requests, responses, status codes, and cookies
- HTML forms and the FormData API
Three ways to use React Router
React Router v7 ships as one package, react-router, with three modes. Each mode adds features on top of the previous one.
| Mode | You write | React Router provides |
|---|---|---|
| Declarative | `<BrowserRouter>` and `<Routes>` in JSX | URL matching, `Link`, `Outlet`, navigation hooks |
| Data | `createBrowserRouter` with route objects | Everything above, plus loaders, actions, and pending state |
| Framework | An `app/routes.ts` file and route modules | Everything above, plus a Vite build, server rendering, generated types, and code splitting |
In declarative and data mode, your code runs only in the browser. You choose your own bundler and write your own server. In framework mode, React Router becomes a Vite-based toolchain. It builds your app for both the server and the browser.
What the framework adds
Framework mode builds on data mode, so loaders, actions, and error boundaries work the same way. It adds these things:
- Route modules. One file holds a route's component, its
loader, itsaction, itsErrorBoundary, and its metadata. Because these live together, you can see everything a URL needs in one place. - A route config file.
app/routes.tsmaps URL paths to route module files. - Server rendering. The first page load arrives as HTML, and then React hydrates it. Hydration means React attaches event handlers to HTML that already exists.
- Code splitting. Each route becomes its own bundle, so the browser downloads only what the current page needs.
- Generated types. React Router creates a
+typesfile for each route, soloaderDataandparamsare typed. - Deployment options. You can render on the server, prerender pages at build time, or ship a single-page app.
How a request flows
Here is what happens when a browser requests a URL.
After hydration, clicking a link does not reload the page. The browser asks the server for the next route's loader data and renders the new route on the client.
A first look at a route module
This route module loads one record and displays it. Later chapters explain each part in detail.
import type { Route } from "./+types/detail";
import { getContact } from "~/contacts.server";
export async function loader({ params }: Route.LoaderArgs) {
const contact = await getContact(params.contactId);
return { contact };
}
export default function ContactDetail({ loaderData }: Route.ComponentProps) {
return <h1>{loaderData.contact.name}</h1>;
}The loader runs on the server. The component receives its result as loaderData, and the type comes from the generated +types file.
Common mistakes
- Mixing up modes.
createBrowserRouterand<BrowserRouter>belong to the other modes. Framework mode usesroutes.tsinstead. - Expecting browser-only code. A loader runs on the server, so it cannot use
windoworlocalStorage. - Treating it as a different library. Hooks like
useNavigationand components likeLinkare the same ones you know from data mode.
Exercise
Name two things that framework mode adds on top of data mode, and say which file maps URLs to route modules.
Show answer
Any two of these: server rendering, a Vite-based build, code splitting per route, generated route types, or deployment options such as prerendering and SPA output. The file is app/routes.ts.
Exercise
A teammate says a loader in framework mode can read localStorage. Is this correct, and why?
Show answer
No. Loaders run on the server during the initial request and for later navigations, and the server has no localStorage or window object. Code that needs browser storage must run in the browser, for example in a client loader or an effect.