react-router-knowledge-patch
React Router v7 changes since training cutoff — Framework mode (routes.ts, Vite plugin), type-safe route modules, middleware with context API, pre-rendering, SPA/SSR config. Load before working with React Router v7+.
What this skill does
# React Router v7+ Knowledge Patch
Claude's baseline knowledge covers React Router v6 (createBrowserRouter, RouterProvider, loaders/actions, nested routing, data routers) and Remix v2 (file-based routing, Vite plugin, loaders/actions).
This skill provides features from v7.0 (November 2024) through v7.14 (April 2025). React Router v7 merges Remix into React Router.
## Three Modes
| Mode | Entry Point | Use Case |
|------|-------------|----------|
| **Declarative** | `<BrowserRouter>` | Classic client-side routing |
| **Data** | `createBrowserRouter` | Client-side with loaders/actions |
| **Framework** | Vite plugin + `routes.ts` | Full-stack SSR/SPA (all new APIs) |
Framework mode is where all the new APIs live.
## Quick Reference
### routes.ts Helpers
| Helper | Purpose | Example |
|--------|---------|---------|
| `route(path, file, children?)` | Named route | `route("products/:pid", "./product.tsx")` |
| `index(file)` | Index route | `index("./home.tsx")` |
| `layout(file, children)` | Layout wrapper | `layout("./marketing-layout.tsx", [...])` |
| `prefix(path, children)` | Path prefix | `prefix("api", [...])` |
| `flatRoutes()` | File-based (Remix-style) | `import { flatRoutes } from "@react-router/fs-routes"` |
All from `@react-router/dev/routes`. See [references/framework-mode.md](references/framework-mode.md).
### routes.ts Example
```ts
import { type RouteConfig, route, index, layout, prefix } from "@react-router/dev/routes";
export default [
index("./home.tsx"),
route("products/:pid", "./product.tsx"),
layout("./marketing-layout.tsx", [
route("about", "./about.tsx"),
]),
...prefix("api", [
route("users", "./api/users.tsx"),
]),
] satisfies RouteConfig;
```
### react-router.config.ts Example
```ts
import type { Config } from "@react-router/dev/config";
export default {
ssr: true, // false for SPA mode
prerender: ["/", "/about"], // or true for all static paths, or async function
appDirectory: "app",
buildDirectory: "build",
} satisfies Config;
```
Pre-render can also be async: `prerender: async ({ getStaticPaths }) => [...getStaticPaths(), ...dynamicPaths]`.
### Generated Route Types
| Type | Use For |
|------|---------|
| `Route.LoaderArgs` | Server loader parameters |
| `Route.ActionArgs` | Server action parameters |
| `Route.ClientLoaderArgs` | Client loader parameters |
| `Route.ClientActionArgs` | Client action parameters |
| `Route.ComponentProps` | Route component props (`loaderData`, `actionData`, `params`, `matches`) |
| `Route.ErrorBoundaryProps` | Error boundary props |
| `Route.HydrateFallbackProps` | Hydration fallback props |
| `Route.MiddlewareFunction` | Server middleware type |
| `Route.ClientMiddlewareFunction` | Client middleware type |
Import per-route: `import type { Route } from "./+types/product"`. Requires `"rootDirs": [".", "./.react-router/types"]` in tsconfig.
### Type-Safe Route Module Example
```ts
import type { Route } from "./+types/product";
export async function loader({ params }: Route.LoaderArgs) {
// params.pid is typed as string
return { product: await getProduct(params.pid) };
}
export default function Product({ loaderData }: Route.ComponentProps) {
return <div>{loaderData.product.name}</div>;
}
```
Components receive typed props (`loaderData`, `actionData`, `params`, `matches`) directly — can replace `useLoaderData()`/`useParams()` hooks. See [references/type-safe-routes.md](references/type-safe-routes.md).
### react-router.config.ts
| Option | Type | Purpose |
|--------|------|---------|
| `ssr` | `boolean` | `true` for SSR, `false` for SPA mode |
| `prerender` | `string[] \| boolean \| async fn` | Static pre-rendering paths |
| `appDirectory` | `string` | App source dir (default: `"app"`) |
| `buildDirectory` | `string` | Build output dir (default: `"build"`) |
| `future` | `object` | Future flags (e.g., `{ v8_middleware: true }`) |
See [references/framework-mode.md](references/framework-mode.md).
### Middleware (v8_middleware future flag)
| Concept | Detail |
|---------|--------|
| Enable | `future: { v8_middleware: true }` in config |
| Context | `createContext<T>()` from `"react-router"` |
| Set context | `context.set(myContext, value)` |
| Get context | `context.get(myContext)` in loaders/actions |
| Server export | `export const middleware: Route.MiddlewareFunction[]` |
| Client export | `export const clientMiddleware: Route.ClientMiddlewareFunction[]` |
| Data mode | Attach `middleware` array to route objects |
### Middleware Example
```ts
import { createContext } from "react-router";
const userContext = createContext<User | null>(null);
export const middleware: Route.MiddlewareFunction[] = [
async function auth({ request, context }, next) {
const user = await getUser(request);
if (!user) throw redirect("/login");
context.set(userContext, user);
let response = await next();
return response;
},
];
export async function loader({ context }: Route.LoaderArgs) {
const user = context.get(userContext);
return { profile: await getProfile(user) };
}
```
See [references/middleware.md](references/middleware.md) for client middleware, Data mode, and custom server setup.
## Topic References
- [Framework Mode](references/framework-mode.md) — routes.ts config, file-based routing, react-router.config.ts, pre-rendering
- [Type-Safe Routes](references/type-safe-routes.md) — Auto-generated types, Route.* types, tsconfig setup, component props
- [Middleware](references/middleware.md) — Server/client middleware, typed context API, Data mode integration
Related in Web Dev
generating-lwc-components
IncludedLightning Web Components with PICKLES methodology and 165-point scoring. Use this skill when the user creates or edits LWC components, builds wire service patterns, or writes Jest tests for LWC. TRIGGER when: user creates/edits LWC components, touches lwc/**/*.js, .html, .css, .js-meta.xml files, or asks about wire service, SLDS, or Jest LWC tests. DO NOT TRIGGER when: Apex classes (use generating-apex), Aura components, or Visualforce.
tanstack-query
IncludedManage server state in React with TanStack Query v5. Set up queries with useQuery, mutations with useMutation, configure QueryClient caching strategies, implement optimistic updates, and handle infinite scroll with useInfiniteQuery. Use when: setting up data fetching in React projects, migrating from v4 to v5, or fixing object syntax required errors, query callbacks removed issues, cacheTime renamed to gcTime, isPending vs isLoading confusion, keepPreviousData removed problems.
document-processor-api
IncludedProcess documents with Nutrient DWS. Use when the user wants to generate PDFs from HTML or URLs, convert Office/images/PDFs, assemble or split packets, OCR scans, extract text/tables/key-value pairs, redact PII, watermark, sign, fill forms, optimize PDFs, or produce compliance outputs like PDF/A or PDF/UA. Triggers include convert to PDF, merge these PDFs, OCR this scan, extract tables, redact PII, sign this PDF, make this PDF/A, or linearize for web delivery.
nutrient-document-processing
IncludedProcess documents with Nutrient DWS. Use when the user wants to generate PDFs from HTML or URLs, convert Office/images/PDFs, assemble or split packets, OCR scans, extract text/tables/key-value pairs, redact PII, watermark, sign, fill forms, optimize PDFs, or produce compliance outputs like PDF/A or PDF/UA. Triggers include convert to PDF, merge these PDFs, OCR this scan, extract tables, redact PII, sign this PDF, make this PDF/A, or linearize for web delivery.
tanstack-query
IncludedManage server state in React with TanStack Query v5. Covers useMutationState, simplified optimistic updates, throwOnError, network mode (offline/PWA), and infiniteQueryOptions. Use when setting up data fetching, fixing v4→v5 migration errors (object syntax, gcTime, isPending, keepPreviousData), or debugging SSR/hydration issues with streaming server components.
accelint-nextjs-best-practices
IncludedNext.js performance optimization and best practices. Use when writing Next.js code (App Router or Pages Router); implementing Server Components, Server Actions, or API routes; optimizing RSC serialization, data fetching, or server-side rendering; reviewing Next.js code for performance issues; fixing authentication in Server Actions; or implementing Suspense boundaries, parallel data fetching, or request deduplication.