Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 50 ยท Getting Started

Installing and Your First Routes

Install React Router 7 in a Vite project, wrap the app in BrowserRouter and build a two-page app with Routes, Route and Link, plus a runnable stand-in.

The best way to learn a router is to build the smallest app that has more than one page. In this lesson you will install React Router in a fresh Vite project, wrap your app in a router, declare two routes and link between them. That is declarative mode, the simplest of the three modes from What Is React Router?, and everything later in this course builds on these four ingredients: BrowserRouter, Routes, Route and Link.

The real project steps are shown read-only, because the live editor cannot run npm. Alongside them you get a runnable stand-in with the same shape (<Routes> containing <Route> elements), so you can see the idea work.

Step 1: create a project and install the package

You need a current Node.js LTS release and a terminal. Vite is the usual build tool for a React app that does not use a framework:

Terminal
npm create vite@latest my-router-app -- --template react
cd my-router-app
npm install
npm install react-router

This is read-only: run it on your own machine. The last command adds one dependency. In version 7 the package is react-router, and it contains everything for the browser as well. Older tutorials ask for react-router-dom; that package still exists and re-exports the same things, so either import works in an existing project, but new code should use react-router.

Start the dev server with npm run dev. Vite serves your index.html for any path, so a URL such as http://localhost:5173/about will load your app instead of a 404. Production servers need the same behavior, which the lesson How Routing Works Under the Hood explains.

Step 2: wrap the app in a router

A router object holds the current location and notifies React when it changes. Every component that uses a link or a routing hook has to be rendered inside one, so you put it at the very top of the tree. BrowserRouter is the router for normal websites: it uses the browser's History API, so URLs look like /about and not /#/about.

JSX
// src/main.jsx (real React Router, read-only)
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import { BrowserRouter } from 'react-router';
import App from './App.jsx';

createRoot(document.getElementById('root')).render(
  <StrictMode>
    <BrowserRouter>
      <App />
    </BrowserRouter>
  </StrictMode>,
);

If you forget this wrapper, the first <Link> or routing hook you render throws an error that says the hook may be used only in the context of a Router. It is the most common error for beginners, and the fix is always the same: render a router above the component.

Step 3: declare your routes

Now describe which component belongs to which path. Routes looks at the current location and renders exactly one of its Route children (and, later, nested ones). Each Route has a path and an element: the React element to render when the path matches.

JSX
// src/App.jsx (real React Router, read-only)
import { Routes, Route, Link } from 'react-router';
import Home from './pages/Home.jsx';
import About from './pages/About.jsx';

export default function App() {
  return (
    <div>
      <nav>
        <Link to="/">Home</Link> | <Link to="/about">About</Link>
      </nav>

      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
      </Routes>
    </div>
  );
}
JSX
// src/pages/About.jsx (read-only)
export default function About() {
  return (
    <main>
      <h1>About</h1>
      <p>This page lives at /about.</p>
    </main>
  );
}

Four things deserve attention:

  • element takes an element, not a component. You write element={<About />} with angle brackets, because you pass a ready-made React element. React Router also accepts a Component prop (Component={About}) that takes the component function itself. Both work. The element prop is the classic style, and Component is what route objects in data mode usually use.
  • <Routes> renders one match. Unlike version 5, you do not need a <Switch>, and the order of the routes does not decide the winner. The router ranks the routes by specificity, which Routes and Matching covers.
  • <Link> replaces <a>. It renders a real anchor with the right href, but a normal click changes the URL without reloading the page. The details are in Link and NavLink.
  • Everything outside <Routes> stays on screen. The <nav> above is rendered for every page. That is the seed of layouts, which you will build properly in Nested Routes and Outlet.
flowchart TD
  R["BrowserRouter<br/>(owns the location)"] --> A["App"]
  A --> N["nav with Links<br/>always visible"]
  A --> RS["Routes"]
  RS -->|"path is /"| H["Home"]
  RS -->|"path is /about"| B["About"]

Only the branch under Routes changes when the URL changes; the navigation stays mounted.

Try it: the same app with a stand-in

The stand-in below keeps the API shape of the real library. Route is a marker component whose props Routes reads, exactly like the real thing, and Link produces a hash URL because the sandbox cannot change real paths. The app code, from the <nav> down, is what you would write with the real library, apart from the imports.

A two-page app with Routes, Route and Link

Click "About" and look at the address of the preview: it ends with #/about. With real React Router and BrowserRouter it would end with /about. Notice that Routes renders only the element of the first match, and that the navigation does not disappear when the page changes.

A page that does not exist

Nothing yet handles a URL that matches no route: the real <Routes> renders nothing for it (and logs a warning in the console, "No routes matched location"), leaving a blank area. The standard fix is a catch-all route with path="*", shown in the next TryIt and covered fully in 404 Pages and Catch-All Routes.

JSX
// Real React Router, read-only
<Routes>
  <Route path="/" element={<Home />} />
  <Route path="/about" element={<About />} />
  <Route path="*" element={<NotFound />} />
</Routes>

The stand-in below adds a third page, the * route, and a small "visited pages" list that proves navigation does not reload the app: the list lives in React state, and a real page reload would erase it.

A catch-all route and navigation without reloads

Click around, including the dead link. The list keeps growing, because the app never reloaded and its state survived every navigation. That is the central promise of a client-side router. The stand-in checks * only after the exact routes, which is a very small version of the ranking that the real router does.

Common first-day mistakes

  • No router above the component. Any component that renders <Link> or calls a routing hook needs a router somewhere above it.
  • Using plain <a href="/about"> for internal links. It works, but it reloads the whole page and throws away your state. Use <Link> for navigation inside the app, and <a> only for outside sites.
  • Writing element={About}. That passes a function where an element belongs. Use element={<About />} or Component={About}.
  • Opening a deep URL on a production server and getting a 404. The server has to return index.html for every path. Vite's dev server already does; your host may need a rewrite rule.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which component must be rendered above any Link or routing hook?

    BrowserRouter owns the location and makes it available to links and hooks. Without a router above them, they throw an error.
  2. 2. What is the difference between element and Component on a Route?

    Both render the same thing. The element prop expects an already created element, and the Component prop expects the component function, which the router creates for you.
  3. 3. Why use Link instead of a plain anchor for an internal page?

    A plain anchor makes the browser load a new document. Link renders an anchor too, but intercepts ordinary clicks and tells the router to navigate, keeping the running app and its state.
  4. 4. What does a route with path="*" do in this app?

    A star is a catch-all. Since the router ranks it below more specific routes, it only wins when nothing else matches, which makes it the natural place for a 404 page.

Summary

  • Install the package with npm install react-router; version 7 uses react-router for everything, and react-router-dom is only a re-export.
  • Wrap your app once in BrowserRouter, declare pages with <Routes> and <Route path element>, and navigate with <Link to>.
  • Pass element={<Page />} (an element) or Component={Page} (a function), never element={Page}.
  • Content outside <Routes> stays visible on every page, which is the start of layouts.
  • Add path="*" as the last route to handle unknown URLs, and make sure your production server returns index.html for every path.
  • The live stand-in keeps the same API shape but uses hash URLs because the sandbox cannot use the History API.

Next: How Routing Works Under the Hood