Why React Router v7?
React Router v7 launched in December 2024 as the merger of Remix and React Router, bringing modern framework features to React applications: Type-Safe Routing - Auto-generated types for routes, loaders, and actions with full end-to-end type safety. File-Based Routing - Organize routes by creating files in the routes/ directory. No manual route configuration needed. Built-In Data Loading - Loaders run before routes render, providing data with guaranteed type safety. Type-Safe Actions - Handle form submissions with type-safe actions that integrate with loaders. Error Boundaries - Per-route error handling with automatic error boundary generation. Optimized Builds - Automatic code splitting, route-based prefetching, and Vite-powered builds. Static Export - Can generate static HTML files for serving with Mizu backend. Progressive Enhancement - Works without JavaScript, enhances with React hydration.React Router v7 vs Other Frameworks
| Feature | React Router v7 | React + RR v6 | Next.js | Remix |
|---|---|---|---|---|
| Type Safety | β Auto-generated | β οΈ Manual | β οΈ Manual | β Built-in |
| Routing | File-based | Manual | File-based | File-based |
| Data Loading | Built-in loaders | Manual fetch | getStaticProps | Loaders |
| Bundle Size | ~50kB | ~45kB | ~90kB | ~50kB |
| Learning Curve | β οΈ Moderate | β Easy | β οΈ Moderate | β οΈ Moderate |
| Static Export | β Yes | β Yes | β Yes | β No |
| SSR | β οΈ Optional | β No | β Yes | β Yes |
| Best for | Data-heavy SPAs | Simple SPAs | Full-stack | SSR apps |
| Company | Remix/Shopify | Independent | Vercel | Remix/Shopify |
- You want modern framework features with React Router familiarity
- Type safety from routes to data is important
- You need built-in data loading patterns
- File-based routing appeals to you
- Youβre building a data-heavy SPA with static export
- You want the option to add SSR later
- You need the simplest possible setup β Use React + React Router v6
- You need full SSR now β Use Next.js or full Remix
- Bundle size is critical β Use Preact
- You prefer Vue β Use Vue Router or Nuxt
Quick Start
Create a new React Router v7 project with the CLI:http://localhost:3000 to see your app!
Project Structure
How React Router v7 Works with Mizu
React Router v7 integrates seamlessly with Mizu through static export mode:- User requests
http://yourdomain.com - Mizu serves
index.htmlfrom embedded FS - Browser loads React Router bundles
- React Router hydrates and takes over
- Client-side routing handles navigation
- API calls go to Mizu Go handlers
Backend Setup
app/server/app.go
app/server/routes.go
Frontend Setup
File-Based Routing
React Router v7 uses file-based routing conventions:_index.tsxβ Index route (matches parent path exactly)_layout.tsxβ Layout component (wraps child routes)$id.tsxβ Dynamic segment (captures param).tsxβ Route file
Route Configuration
app/routes.ts
Define your route tree programmatically:
/β _layout β _index/aboutβ _layout β about/usersβ _layout β users/_layout β users/_index/users/:idβ _layout β users/_layout β users/$id
Root Component
app/root.tsx
Layoutwraps all pages (renders once)Metarenders all route meta tagsLinksrenders all route link tags (stylesheets, etc.)Scriptsincludes React Router scriptsScrollRestorationremembers scroll positions
Data Loading with Loaders
React Router v7βs killer feature is type-safe data loading:Basic Loader
Loader with Params
- Data loads before rendering (no loading states)
- Full type safety from loader to component
- Errors handled by error boundaries
- Can be cached and revalidated
- Run in parallel for nested routes
Error Handling
Each route can have its own error boundary:Meta Tags and SEO
Define meta tags per route:- Access to loader data (
data) - Access to URL params (
params) - Fully typed with Route.MetaArgs
- Rendered in
<head>by<Meta />component
Navigation
Links
Programmatic Navigation
Forms and Actions
React Router v7 provides type-safe form handling:Basic Form
- Uses Web Forms API (works without JS)
- Automatic revalidation after submission
- Loading states via
useNavigation() - Progressive enhancement
- Type-safe action data
Optimistic UI
TypeScript Integration
React Router v7 generates types automatically:Type Generation
Runnpm run typecheck to:
- Generate route types in
app/+types/ - Type check your entire app
Using Generated Types
Configuration
react-router.config.ts
vite.config.ts
Development Workflow
Start Development
http://localhost:3000
Making Changes
Frontend changes:- Edit any
.tsxfile - Save the file
- Browser updates instantly (HMR)
- Types regenerate automatically
- Edit
.gofiles - Stop server (Ctrl+C)
- Restart with
go run cmd/server/main.go
Type Checking
- Generates route types
- Runs TypeScript compiler
- Shows any type errors
Building for Production
Build the complete app:- Runs
react-router buildin frontend/ - Generates static HTML for all routes
- Outputs to
dist/directory - Ready to embed in Go binary
Build Optimizations
Code splitting: React Router automatically splits code by route. Each route loads only when navigated to. Prefetching:intent: Prefetch on hoverrender: Prefetch on rendernone: No prefetch (default)
dist/ for bundle sizes.
Advanced Features
Nested Layouts
Create shared layouts for route groups:Route Grouping
Use. for pathless layouts:
Splat Routes
Catch-all routes:/anything/not/matched
Resource Routes
API-only routes (no UI):Real-World Example: User Management
Complete example with CRUD operations:List Users
View User Detail
Performance Tips
1. Use Loaders for Data
β Donβt fetch in components:2. Prefetch Important Routes
3. Use React.memo for Static Content
4. Code Split Large Components
Troubleshooting
Types Not Generating
Problem:+types/ folder not updating
Solution:
404 on Page Refresh
Problem: Direct URLs work in dev, 404 in production Solution: Ensure React Router config hasssr: false:
HMR Not Working
Problem: Changes donβt appear in browser Solution: Check Vite dev server is running on correct port:Build Errors
Problem:react-router build fails
Solution:
- Check all imports are valid
- Run
npm run typecheckto find type errors - Check loader/action return types
- Ensure all route files export default component
Loader Data is Undefined
Problem:loaderData is undefined in component
Solution:
- Ensure loader exports are async functions
- Check loader returns an object (not just a value)
- Verify loader path in
routes.tsis correct
Deployment
Production Build
Docker Deployment
Migration from React Router v6
Migrating is straightforward:1. Update Dependencies
2. Move Routes to Files
Before (v6):3. Convert to Loaders
Before:4. Add Type Generation
Createroutes.ts:
When to Choose React Router v7
Choose React Router v7 When:
β You want modern framework features without SSR complexity β Type-safe routing and data loading is important β Youβre familiar with React Router and want the next evolution β File-based routing appeals to you β You need built-in data loading patterns β Static export + Go backend is your deployment model β You might want to add SSR later (easy upgrade path)Choose Something Else When:
- Simplest possible setup β Use React + React Router v6
- Need SSR now β Use full Remix or Next.js
- Prefer manual routing β Use React + React Router v6
- Bundle size is critical β Use Preact
- Team prefers Vue β Use Vue Router or Nuxt