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.
React Router for beginners: learn routes, Link, and useParams with small copy-paste examples. Build a multi-page React app in plain, simple steps.
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.
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:
Open your React project in a terminal and run this command. It uses npm, the tool that installs JavaScript packages.
npm install react-router-domThis 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.
A page is just a component. Make two small ones.
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.
Now we tell React Router which URL shows which page.
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".
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.
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:
<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.
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.
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".
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.
| Tool | What it does | Example |
|---|---|---|
| BrowserRouter | Enables routing for the app | <BrowserRouter>...</BrowserRouter> |
| Routes / Route | Maps a URL to a component | <Route path="/about" element={<About />} /> |
| Link | Navigates without reload | <Link to="/about">About</Link> |
| useParams | Reads values from the URL | const { id } = useParams(); |
| path="*" | Catches unknown URLs | <Route path="*" element={<NotFound />} /> |
New to the building blocks? Read how React components work, then come back and turn them into pages.
Read: React components for beginnersReact 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.
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.
Yes. React does not include routing. Run npm install react-router-dom in your project and then import what you need.
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.
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 FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
Learn SQL AND, OR and NOT operators with simple examples. See how to combine conditions in WHERE and avoid the common mistakes beginners make.
Learn JavaScript template literals with simple examples: backticks, ${} variables, and multi-line strings. A beginner-friendly guide you can copy and run.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.