Sub topic 2
Creating a project and reading its layout
This chapter assumes you know React components and hooks, basic TypeScript, Node.js and npm, and how HTTP requests, responses and cookies work. You will create a React Router framework mode project, run it, and learn what each generated file does. The chapter does not cover route configuration, loaders or actions in detail. Later chapters cover those.
Creating the project
React Router ships a project template. You need a current Node.js LTS release and npm.
npx create-react-router@latest my-app
cd my-app
npm install
npm run devThe create command asks whether to initialize a git repository and whether to install dependencies. If you answer yes to the install question, you can skip npm install. The dev server starts at http://localhost:5173. Vite is the build tool behind it, so the port is Vite's default.
If port 5173 is taken, Vite picks the next free port and prints it in the terminal. Read the terminal output instead of assuming the port.
What the template generates
A fresh project looks like this:
my-app/
├── app/
│ ├── routes/
│ │ └── home.tsx
│ ├── app.css
│ ├── root.tsx
│ ├── routes.ts
│ └── welcome/
├── public/
│ └── favicon.ico
├── .gitignore
├── package.json
├── react-router.config.ts
├── tsconfig.json
└── vite.config.tsThe exact contents of app/welcome/ and the styling setup can change between template versions. The structure of the top-level files stays stable.
| File or folder | Purpose |
|---|---|
| `app/root.tsx` | The root route. It renders the HTML document and wraps every other route. |
| `app/routes.ts` | Maps URL paths to route module files. |
| `app/routes/` | Route modules. A route module is a file that exports a component and, optionally, a loader, an action and other route exports. |
| `react-router.config.ts` | Framework settings such as `ssr` and `prerender`. |
| `vite.config.ts` | Vite configuration, including the React Router plugin. |
| `public/` | Static files served as-is from the site root. |
| `.react-router/` | Generated types. It appears after you run the dev server or `npm run typecheck`. It is git-ignored. |
The scripts in package.json
The template defines these scripts:
{
"scripts": {
"build": "react-router build",
"dev": "react-router dev",
"start": "react-router-serve ./build/server/index.js",
"typecheck": "react-router typegen && tsc"
}
}devruns the Vite dev server with hot module replacement.buildproduces abuild/directory withclient/andserver/subfolders.startserves the production build with the built-in Node server.typecheckgenerates route types, then runs the TypeScript compiler.
How the pieces fit together
Framework mode is a Vite plugin plus a runtime. The plugin reads app/routes.ts, finds each route module, and builds two bundles from the same source files. One bundle runs on the server. The other runs in the browser.
Hydration is the step where React attaches event handlers to HTML that the server already rendered. After hydration, link clicks and form submissions are handled in the browser without full page loads.
Reading the key files
The route config
Open app/routes.ts. The template contains a single route:
import { type RouteConfig, index } from "@react-router/dev/routes";
export default [index("routes/home.tsx")] satisfies RouteConfig;index("routes/home.tsx") says that the URL / renders app/routes/home.tsx. Paths in this file are relative to the app directory. The satisfies RouteConfig clause checks the shape of the array without widening its type.
The root route
app/root.tsx exports a Layout function, a default App component and, usually, an ErrorBoundary. The Layout function renders the <html> element. The default export renders an <Outlet />, which is the place where the matched child route appears. The next chapters cover this file in detail.
A route module
Open app/routes/home.tsx. A minimal version looks like this:
import type { Route } from "./+types/home";
export function meta({}: Route.MetaArgs) {
return [{ title: "My App" }];
}
export default function Home() {
return <h1>Hello</h1>;
}The import from ./+types/home looks like a path to a real file. It is not. The framework generates it in .react-router/types/ and TypeScript resolves it through the rootDirs setting in tsconfig.json. If your editor reports that the module is missing, run npm run typecheck or start the dev server once so the types are generated.
The framework config
import type { Config } from "@react-router/dev/config";
export default {
ssr: true,
} satisfies Config;With ssr: true, the server renders HTML for each request. Setting it to false produces a client-only single-page app. A later chapter compares these modes.
Try it
Edit app/routes/home.tsx and change the heading. The browser updates without a full reload. Then add a second file and register it:
export default function About() {
return <h1>About</h1>;
}Save this as app/routes/about.tsx. Then change app/routes.ts:
import { type RouteConfig, index, route } from "@react-router/dev/routes";
export default [
index("routes/home.tsx"),
route("about", "routes/about.tsx"),
] satisfies RouteConfig;Visit /about. The page renders. Creating the file alone does nothing, because the config is the only source of routes.
Common errors
- Route file not found. The path in
routes.tsis relative toapp/, not toroutes.tsitself. Writing"./routes/about.tsx"works, but"app/routes/about.tsx"does not. - Missing
+typesmodule. Generated types are absent until typegen runs. Runnpm run typecheck. - A file in
app/routes/does not show up. The default template does not use file-based routing. A file only becomes a route whenroutes.tslists it. - Wrong Node version. Older Node releases fail with syntax or module errors from Vite. Upgrade to a current LTS release.
Exercise
You create app/routes/pricing.tsx with a default-exported component and visit /pricing, but the page shows a 404. What is the most likely cause, and how do you fix it?
Show answer
The file is not registered. This template does not infer routes from the folder, so app/routes.ts must list it. Add route("pricing", "routes/pricing.tsx") to the exported array.
Exercise
Your editor reports that ./+types/home cannot be found, but the app runs. Why, and what command fixes the editor error?
Show answer
The +types modules are generated into .react-router/types/. They do not exist until typegen runs. Run npm run typecheck (which calls react-router typegen) or start npm run dev so the types are generated.