Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 50 ยท Getting Started

What Is React Router?

Learn what React Router does, why single-page apps need a router, how version 7 merged with Remix, and what declarative, data and framework mode mean.

A website without a router has one address. You open it, click around, and the URL never changes. That breaks everything people expect from the web: you cannot bookmark a screen, you cannot send a friend a link to a product, the back button leaves your app instead of stepping back one screen, and search engines see a single page. A router is the piece of software that fixes this. It keeps the URL and the user interface in sync: the URL decides what is on screen, and moving around the screen changes the URL.

React Router is the most widely used router for React. It started in 2014, and it is the library that most React developers mean when they say "routing". Version 7, the current major version, is both a small library for matching URLs to components and a full-stack framework. This course takes you from the first <Route> to nested layouts, loaders, actions, protected pages, code splitting, server rendering and testing.

If you have not built a router yourself, read Client-Side Routing in the React tutorial first. You will not need it to follow this lesson, but it makes the rest of the course feel less like magic.

What a router actually does

Every router, in every framework, does the same four jobs:

  1. Read the location. The URL has a path (/products/7), an optional query string (?sort=price) and an optional hash (#reviews).
  2. Match. It compares the path against a list of route patterns, such as /products/:id, and extracts the dynamic parts.
  3. Render. It shows the component that belongs to the matched route, often inside layouts that stay on screen while only the inner part changes.
  4. Navigate. It gives you links and functions that change the URL without reloading the page, and it keeps the browser's back and forward buttons working.
flowchart LR
  U["URL<br/>/products/7"] --> M["Match routes"]
  M --> P["Params<br/>id = 7"]
  M --> C["Components<br/>Layout > ProductPage"]
  C --> S["Screen"]
  L["Link click or<br/>navigate()"] --> U

The router turns a URL into a component tree, and user actions turn back into URL changes.

Everything else React Router offers (loading data, handling form submissions, showing pending states, catching errors) is built on top of these four jobs.

The three modes of version 7

React Router 7 is one package, react-router, that you can use at three levels. The documentation calls them modes, and understanding them early saves a lot of confusion, because articles and Stack Overflow answers mix them freely.

Mode How you start it What you get
Declarative <BrowserRouter> with <Routes> and <Route> Matching, links, navigation hooks. Nothing else.
Data createBrowserRouter([...]) and <RouterProvider> Everything above, plus loader, action, <Form>, fetchers, pending state and error boundaries on each route.
Framework The Vite plugin and a routes.ts file Everything above, plus server rendering, type-safe route modules, automatic code splitting and deployment adapters.

Each mode is a superset of the one before. You can start with declarative routing in an afternoon, move to data mode when your screens need data, and move to framework mode if you want server rendering. Lesson Choosing Between Declarative, Data and Framework Mode helps you decide, and the later chapters of this course cover each mode in turn.

Here is the same tiny app in the first two modes. This is the real React Router API, shown read-only because the live editor on this site cannot install npm packages:

JSX
// Declarative mode (real React Router, read-only)
import { BrowserRouter, Routes, Route } from 'react-router';

export default function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/products/:id" element={<ProductPage />} />
      </Routes>
    </BrowserRouter>
  );
}
JSX
// Data mode (real React Router, read-only)
import { createBrowserRouter } from 'react-router';
import { RouterProvider } from 'react-router/dom';

const router = createBrowserRouter([
  { path: '/', Component: Home },
  {
    path: '/products/:id',
    loader: ({ params }) => fetchProduct(params.id),
    Component: ProductPage,
  },
]);

export default function App() {
  return <RouterProvider router={router} />;
}

The only visible difference is that the second version has a loader: a function the router runs before it shows the page, so the component receives its data already loaded instead of fetching it in an effect.

A short history

Knowing where the library comes from explains why some names look odd.

  • Versions 1 to 3 (2014 to 2016) were configured with nested route objects and were already popular, but each major version changed the API a lot.
  • Version 4 (2016) was a rewrite. Routes became ordinary components that you rendered anywhere in the tree.
  • Version 5 (2017) refined version 4. It used <Switch> to pick the first matching route, useHistory for navigation and the component and render props.
  • Version 6 (2021) brought <Routes> instead of <Switch>, relative paths, route ranking that no longer depends on order, useNavigate and nested routes with <Outlet>. A year later, 6.4 added the data APIs: createBrowserRouter, loaders and actions.
  • Remix was a separate full-stack framework, built by the same authors, on top of React Router. It put loaders and actions on routes and rendered on the server.
  • Version 7 (2024) merged the two projects. The framework features of Remix moved into React Router, and the next major version of Remix is a different, smaller project. What used to be "Remix v2" is now React Router framework mode.

Two practical consequences. First, in version 7 you import almost everything from react-router. The older package react-router-dom still exists as a thin re-export, so existing code keeps working. Second, if an old tutorial says useHistory or <Switch>, it is describing version 5. Our migration guide lists the replacements.

How the live examples in this course work

This site runs your code in a sandboxed frame. The sandbox deliberately blocks history.pushState and storage, and it cannot install packages such as react-router. So this course follows one rule in every lesson:

  • Real React Router code is read-only. It appears in plain jsx code blocks that you can copy into a project on your own machine.
  • Runnable examples use a stand-in. It is a small router of about thirty lines, written with React and window.location.hash, that imitates the part of React Router the lesson is about: matching with params, nested layouts with an outlet, a loader-like data function, links and navigation.

The stand-ins are teaching tools, not replacements for the library. They behave like React Router for the situations in each lesson, and every lesson says where they differ. If a runnable example and the real library ever seem to disagree, the library is right.

Because the sandbox cannot use real paths, stand-in URLs use the hash form: #/products/7 instead of /products/7. Run this first stand-in and click through it.

URL in, UI out: a stand-in router

Click the links and watch two things change together: the location line and the page content. That pairing is the whole idea. Notice what the stand-in does not do: it has no way to say "load this product before showing the page", no nested layouts and no ranking of routes. The next example adds the first of those.

Loaders: where data mode starts

In declarative mode a route is only a path and an element. In data mode a route may also carry a loader. This stand-in runs a loader-like function when a route matches and hands the result to the component through a hook shaped like React Router's useLoaderData. The real library runs loaders asynchronously, in parallel, before rendering. The stand-in runs a synchronous function so that the example is deterministic.

A route with a loader-like function

Open "Product 2" and the component simply reads its data; open "Product 99" and the loader throws, so the failure is shown instead of a broken page. In the real library, the equivalent is an errorElement on the route, and you will build that in the data mode chapter. The important design shift is where the data code lives: next to the route, not inside the component.

Is React Router the right choice?

For most React projects that are not built on a full-stack framework such as Next.js, yes. Other routers exist: TanStack Router offers fully typed paths and search params, and tiny routers such as Wouter fit very small apps. React Router's advantages are its huge ecosystem, a gradual path from simple routing to a full framework, and the fact that nearly every React job description assumes you know it.

If your project already uses Next.js, you do not need React Router. Next.js has its own file-based router. See the Next.js section in React Router and Framework Routers for a comparison.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What are the four jobs every router does?

    A router reads the URL, matches it against route patterns, renders the matching components and gives you links and functions to change the URL without a full page load.
  2. 2. Which mode adds loaders, actions and error boundaries to routes but does not require server rendering?

    Data mode (createBrowserRouter with RouterProvider) lets each route carry a loader, an action and an errorElement. Framework mode adds a build plugin and server rendering on top of it.
  3. 3. What happened to Remix in version 7?

    React Router 7 merged the two projects. Remix's full-stack features (loaders, actions, server rendering, route modules) now live in React Router's framework mode.
  4. 4. Why are the runnable examples in this course based on a stand-in router?

    The sandbox blocks package installs and the History API. The stand-ins use window.location.hash and React to teach the same ideas, and the real library code is shown read-only.

Summary

  • A router keeps the URL and the UI in sync: it reads the location, matches routes, renders components and navigates without reloading.
  • React Router 7 is one package with three modes: declarative (matching and links), data (adds loaders, actions and error handling) and framework (adds a build plugin and server rendering).
  • Version 7 merged Remix into React Router; import from react-router, and treat useHistory and <Switch> as version 5 leftovers.
  • Real React Router code in this course is read-only, and runnable examples use a small hash-based stand-in that imitates matching, params, loaders and links.
  • Nothing in the stand-ins replaces the library: they exist so that you can see the mechanics.

Next: Installing and Your First Routes