Sub topic 1
What Framework Mode Adds to React Router
Prerequisites
You should know React components, props, hooks, TypeScript basics, and npm. You should also understand URLs and nested HTML elements.
What framework mode adds to React Router
React Router library mode gives you client-side routing. You declare which component renders for each URL.
Framework mode adds an application structure around routing. It provides route modules, server rendering, data loading, form actions, error boundaries, generated route types, and production builds. A route module can connect a URL to its component, server data, and mutations.
Framework mode is useful when the browser and server need to work together. It also gives you conventions for organizing the project instead of making you assemble each feature.
Creating a project and reading its layout
Create a project with the React Router CLI:
npx create-react-router@latest contacts-app
cd contacts-app
npm install
npm run devA typical project includes these files:
app/root.tsx: the document shell and the root route.app/routes.ts: the URL-to-route-module configuration.app/routes/: route modules such as pages and layouts.react-router.config.ts: server rendering and build settings.public/: static files served directly.
Defining routes in `app/routes.ts`
The route configuration maps URL patterns to files. An index route represents the parent URL. A layout route wraps children without adding its own URL segment.
import {
type RouteConfig,
index,
route,
layout,
} from "@react-router/dev/routes";
export default [
index("routes/home.tsx"),
layout("routes/contacts/layout.tsx", [
route("contacts", "routes/contacts/list.tsx"),
route("contacts/:contactId", "routes/contacts/detail.tsx"),
]),
] satisfies RouteConfig;The :contactId segment is a dynamic segment. React Router captures its value for the matching route.
The root route: document shell, Links, Scripts
The root route renders the HTML document and the matched application routes. Links adds route stylesheets and other link tags. Scripts loads the browser runtime.
import { Links, Outlet, Scripts } from "react-router";
export default function Root() {
return (
<html lang="en">
<head>
<meta charSet="utf-8" />
<Links />
</head>
<body>
<Outlet />
<Scripts />
</body>
</html>
);
}Nested routes, layouts, and `Outlet`
A layout route renders shared UI, such as navigation or a sidebar. Its Outlet is the insertion point for the selected child route.
A child route replaces the Outlet content during navigation while the layout remains mounted.
Exercise
What does an Outlet provide in a layout route?
Show answer
It provides the location where the currently matched child route renders.