All posts
ReactNovember 3, 20267 min read

React Router for Beginners: Pages and Links Explained

React Router for beginners: learn routes, Link, and useParams with small copy-paste examples. Build a multi-page React app in plain, simple steps.

F
Fepiq Team
Fepiq

React Router is a library that lets a React app show different pages at different URLs, like /about or /products/5. If you searched for "react router for beginners", this is the short answer: you install the package, wrap your app in a router, and tell it which component to show for each path. This guide walks through it step by step with small examples.

You should already know what a React component is. If not, read our guide to React components first. We define every new word as we go.

What is React Router and why do you need it?

A normal website has many HTML files. Each URL loads a different file. A React app is usually a single page: the browser loads one HTML file and React changes what you see. This is called a single-page application (SPA).

The problem is that users still expect different URLs, a working back button, and links they can share. React Router solves this. It reads the URL in the address bar and shows the matching component. The page does not fully reload, so it feels fast.

Here are the main words you will meet:

  • Router: the wrapper that lets your app know the current URL.
  • Route: a rule that says "when the URL is this, show that component".
  • Link: a clickable link that changes the URL without reloading the page.
  • Page: just a normal React component that we treat as a whole screen.

Step 1: Install React Router

Open your React project in a terminal and run this command. It uses npm, the tool that installs JavaScript packages.

bash
npm install react-router-dom

This downloads the react-router-dom package into your project. You only do this once. The examples below use version 6 or newer, which is what you get today.

Step 2: Create two simple pages

A page is just a component. Make two small ones.

jsx
function Home() {
  return <h1>Welcome to the Home page</h1>;
}

function About() {
  return <h1>About us</h1>;
}

Each function returns some JSX, which is the HTML-like syntax React uses. Home shows a heading with a welcome message. About shows a different heading. Right now nothing connects them to a URL. That comes next.

Step 3: Set up the router and routes

Now we tell React Router which URL shows which page.

jsx
import { BrowserRouter, Routes, Route } from 'react-router-dom';

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
      </Routes>
    </BrowserRouter>
  );
}

export default App;

Line 1 imports three tools. BrowserRouter wraps the whole app so it can watch the URL. Routes holds your list of rules. Each Route has a path (the URL part) and an element (the component to show). The path "/" is the home page. The path "/about" shows the About component. Visit /about in your browser and you will see "About us".

Step 4: Add links with Link

You could use a normal <a> tag, but it reloads the whole page. Use Link instead. It changes the URL and lets React swap the page instantly.

jsx
import { Link } from 'react-router-dom';

function Navbar() {
  return (
    <nav>
      <Link to="/">Home</Link>
      {' | '}
      <Link to="/about">About</Link>
    </nav>
  );
}

Link works like <a>, but it uses the to prop instead of href. Clicking Home goes to "/". Clicking About goes to "/about". The {' | '} part just puts a separator between them.

Place <Navbar /> inside BrowserRouter but above Routes, so it shows on every page:

jsx
<BrowserRouter>
  <Navbar />
  <Routes>
    <Route path="/" element={<Home />} />
    <Route path="/about" element={<About />} />
  </Routes>
</BrowserRouter>

The navbar stays on screen while only the content under it changes. Link must always be used inside a router, or React will show an error.

Step 5: Dynamic routes with useParams

Sometimes the URL contains a changing value, like a product id: /products/1, /products/2. You do not want to write a route for every id. Use a URL parameter, written with a colon.

jsx
import { useParams } from 'react-router-dom';

function Product() {
  const { id } = useParams();
  return <h1>Product number {id}</h1>;
}

// inside <Routes>:
<Route path="/products/:id" element={<Product />} />

The path "/products/:id" matches any value in the last part. The useParams hook (a special React function that starts with "use") returns an object of those values. We pull out id with destructuring. Visit /products/42 and the page says "Product number 42".

Step 6: Show a 404 page for unknown URLs

jsx
function NotFound() {
  return <h1>404 - Page not found</h1>;
}

<Route path="*" element={<NotFound />} />

The star path matches anything that no other route matched. Put it last inside Routes. Now typing a wrong URL shows a friendly message instead of a blank page.

Quick reference

ToolWhat it doesExample
BrowserRouterEnables routing for the app<BrowserRouter>...</BrowserRouter>
Routes / RouteMaps a URL to a component<Route path="/about" element={<About />} />
LinkNavigates without reload<Link to="/about">About</Link>
useParamsReads values from the URLconst { id } = useParams();
path="*"Catches unknown URLs<Route path="*" element={<NotFound />} />

Common beginner mistakes

  • Using <a href> instead of Link, which reloads the page and resets your state.
  • Forgetting to wrap the app in BrowserRouter, which causes an error about the router context.
  • Writing element={Home} instead of element={<Home />}. It needs JSX tags.
  • Mixing old tutorials: version 5 used Switch and component=, but newer versions use Routes and element=.

New to the building blocks? Read how React components work, then come back and turn them into pages.

Read: React components for beginners

Frequently asked questions

What is React Router used for?+

React Router lets a React app show different screens for different URLs without reloading the page. It also makes the browser back button and shareable links work. Almost every multi-page React app uses it or a similar tool.

What is the difference between Link and the a tag in React?+

An <a> tag asks the browser to load a new page, which reloads your whole app and loses its state. Link changes the URL and lets React swap components instantly. Use Link for links inside your app and <a> for outside websites.

Do I need to install React Router separately?+

Yes. React does not include routing. Run npm install react-router-dom in your project and then import what you need.

How do I get a value from the URL in React?+

Define a route with a colon, like /products/:id, then call the useParams hook inside the component. It returns an object, so const { id } = useParams() gives you the value.

What is the difference between BrowserRouter and Routes?+

BrowserRouter is the outer wrapper that tracks the current URL for your whole app. Routes sits inside it and holds your list of Route rules, choosing which one matches.

Want help building your first React app or website? Our team is happy to talk.

Talk to Fepiq

Get new posts in your inbox

Occasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.

Keep reading

Related posts

All posts

Let's build something

Ready to ship your next product with Fepiq?

Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.