ReactEasy
What is React Router?
React Router is the most widely used routing library for React. It maps URLs to components, supports nested layouts, dynamic segments, data loading and mutations, and can also act as a full-stack framework. The current major version is v8 (installed as react-router), a non-breaking upgrade from v7 that requires React 19.2.7+ and Node 22+.
Three ways to use it
- Declarative mode:
<BrowserRouter>,<Routes>,<Route>. Simple client-side routing, as in older versions. - Data mode:
createBrowserRouterwith route objects that haveloader,action,middleware,errorBoundaryandlazy, rendered with<RouterProvider>. Data loads before rendering, avoiding waterfalls. - Framework mode: the successor of Remix. A Vite plugin with file-based or config routes, type-safe route modules, SSR, streaming, prerendering and React Server Components support. Create with
npx create-react-router@latest.
Key APIs
- Components:
Link,NavLink,Outlet,Form,Navigate. - Hooks:
useNavigate,useParams,useSearchParams,useLocation,useLoaderData,useActionData,useNavigation,useFetcher. - Utilities:
redirect,data,createContext(typed middleware context).
v8 changes worth knowing
- The
react-router-dompackage was removed: import everything fromreact-router, and DOM-specific APIs such asRouterProviderfromreact-router/dom. - Former
future.v8_*flags (middleware, split route modules, Vite environment API) are now default behavior. - ESM-only.
- Middleware is stable in data and framework modes.
Alternatives
TanStack Router (type-first), Next.js (framework routing), Wouter (tiny).
Code example
tsx
1// Data mode (React Router v8)2import { createBrowserRouter, Link, Outlet, useLoaderData } from "react-router";3import { RouterProvider } from "react-router/dom";45function Layout() {6 return (7 <>8 <nav><Link to="/">Home</Link> | <Link to="/teams/1">Team 1</Link></nav>9 <Outlet /> {/* child routes render here */}10 </>11 );12}1314function Team() {15 const team = useLoaderData() as { name: string };16 return <h1>{team.name}</h1>;17}1819const router = createBrowserRouter([20 {21 path: "/",22 Component: Layout,23 children: [24 { index: true, Component: () => <h1>Home</h1> },25 {26 path: "teams/:teamId",27 loader: async ({ params }) => fetch(`/api/teams/${params.teamId}`).then((r) => r.json()),28 Component: Team,29 },30 ],31 },32]);3334export const App = () => <RouterProvider router={router} />;
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.