# React Router v7 React Router is a multi-strategy routing library for React that bridges the gap from React 18 to React 19. It serves as both a lightweight client-side routing library and a full-featured React framework with server-side rendering, streaming, and multi-platform deployment capabilities. The library provides platform-agnostic routing primitives through `react-router`, browser-specific bindings through `react-router-dom`, and comprehensive build tooling through `@react-router/dev`. The architecture supports progressive enhancement, starting from basic HTML forms that work without JavaScript, to sophisticated client-side interactions with optimistic UI updates, prefetching, and view transitions. React Router v7 introduces advanced features like Single-Fetch data loading strategy, React Server Components integration, and unified request/response handling across Node.js, Cloudflare Workers, and other platforms through runtime adapters. ## Creating a Basic Router Setup a client-side router with data loading and nested routes. ```tsx import { createBrowserRouter, RouterProvider, useLoaderData } from "react-router-dom"; // Define loaders for data fetching async function rootLoader() { const user = await fetch("/api/user").then(r => r.json()); return { user }; } async function todosLoader({ request, params }) { const url = new URL(request.url); const filter = url.searchParams.get("filter"); const todos = await fetch(`/api/todos?filter=${filter}`).then(r => r.json()); return { todos }; } // Define route configuration const router = createBrowserRouter([ { path: "/", loader: rootLoader, Component: Root, errorElement: , children: [ { index: true, Component: Home, }, { path: "todos", loader: todosLoader, Component: TodosList, }, ], }, ]); function Root() { const { user } = useLoaderData(); return (
{/* Nested routes render here */}
); } function TodosList() { const { todos } = useLoaderData(); return ( ); } export default function App() { return Loading...} />; } ``` ## Form Submission with Actions Handle form submissions with server actions and automatic revalidation. ```tsx import { Form, useActionData, useNavigation, redirect } from "react-router-dom"; // Define action for handling form submissions async function createTodoAction({ request, params }) { const formData = await request.formData(); const title = formData.get("title"); const description = formData.get("description"); // Validate input if (!title || title.length < 3) { return { error: "Title must be at least 3 characters" }; } // Create todo const todo = await fetch("/api/todos", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ title, description }), }).then(r => r.json()); // Redirect to the new todo return redirect(`/todos/${todo.id}`); } const router = createBrowserRouter([ { path: "/todos/new", action: createTodoAction, Component: NewTodoForm, }, ]); function NewTodoForm() { const actionData = useActionData(); const navigation = useNavigation(); const isSubmitting = navigation.state === "submitting"; return (
{actionData?.error &&

{actionData.error}

}