Skip to main content
React Router v7 is the latest evolution of the most popular routing library for React, now merged with Remix to create a powerful full-featured framework. It combines the best of both worlds: React Router’s proven routing with Remix’s data loading patterns, all while maintaining a simple, focused API. This guide shows you how to build production-ready React Router v7 apps with Mizu as your backend.

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

Choose React Router v7 when:
  • 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
Choose something else when:
  • 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:
Visit 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:
At runtime in production:
  1. User requests http://yourdomain.com
  2. Mizu serves index.html from embedded FS
  3. Browser loads React Router bundles
  4. React Router hydrates and takes over
  5. Client-side routing handles navigation
  6. 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:
Naming 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:
This creates the route structure:
  • / β†’ _layout β†’ _index
  • /about β†’ _layout β†’ about
  • /users β†’ _layout β†’ users/_layout β†’ users/_index
  • /users/:id β†’ _layout β†’ users/_layout β†’ users/$id

Root Component

app/root.tsx

Why this structure?
  • Layout wraps all pages (renders once)
  • Meta renders all route meta tags
  • Links renders all route link tags (stylesheets, etc.)
  • Scripts includes React Router scripts
  • ScrollRestoration remembers scroll positions

Data Loading with Loaders

React Router v7’s killer feature is type-safe data loading:

Basic Loader

Loader with Params

Loader benefits:
  • 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:
Errors from loaders are automatically caught!

Meta Tags and SEO

Define meta tags per route:
Meta function features:
  • Access to loader data (data)
  • Access to URL params (params)
  • Fully typed with Route.MetaArgs
  • Rendered in <head> by <Meta /> component

Programmatic Navigation

Forms and Actions

React Router v7 provides type-safe form handling:

Basic Form

Form benefits:
  • 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

Run npm run typecheck to:
  1. Generate route types in app/+types/
  2. Type check your entire app

Using Generated Types

No manual type annotations needed!

Configuration

react-router.config.ts

vite.config.ts

Development Workflow

Start Development

Visit http://localhost:3000

Making Changes

Frontend changes:
  1. Edit any .tsx file
  2. Save the file
  3. Browser updates instantly (HMR)
  4. Types regenerate automatically
Backend changes:
  1. Edit .go files
  2. Stop server (Ctrl+C)
  3. Restart with go run cmd/server/main.go
Or use air for auto-reload.

Type Checking

This:
  1. Generates route types
  2. Runs TypeScript compiler
  3. Shows any type errors

Building for Production

Build the complete app:
This:
  1. Runs react-router build in frontend/
  2. Generates static HTML for all routes
  3. Outputs to dist/ directory
  4. Ready to embed in Go binary
Run in production:

Build Optimizations

Code splitting: React Router automatically splits code by route. Each route loads only when navigated to. Prefetching:
Options:
  • intent: Prefetch on hover
  • render: Prefetch on render
  • none: No prefetch (default)
Bundle analysis:
Check 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:
Matches: /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:
βœ… Use loaders:

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:
This regenerates types from your routes.

404 on Page Refresh

Problem: Direct URLs work in dev, 404 in production Solution: Ensure React Router config has ssr: 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:
  1. Check all imports are valid
  2. Run npm run typecheck to find type errors
  3. Check loader/action return types
  4. Ensure all route files export default component

Loader Data is Undefined

Problem: loaderData is undefined in component Solution:
  1. Ensure loader exports are async functions
  2. Check loader returns an object (not just a value)
  3. Verify loader path in routes.ts is correct

Deployment

Production Build

Docker Deployment

Migration from React Router v6

Migrating is straightforward:

1. Update Dependencies

2. Move Routes to Files

Before (v6):
After (v7):

3. Convert to Loaders

Before:
After:

4. Add Type Generation

Create routes.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

Next Steps

React Guide

Compare with regular React setup

API Integration

Best practices for API communication

Deployment

Build and deploy your app

Next.js Guide

Compare with Next.js

React Router Docs

Official React Router documentation

Remix Migration

Migrate from Remix to React Router v7