In my last project, I built a complete digital-agency website with React. Header, services, footer — everything looked right. But it was one single page. Clicking "Services" in the header just scrolled down. The URL never changed. It wasn't a real website yet.
So I turned it into a real multi-page site with React Router: Home, Services, About, and Contact — plus a 404 page. Here's exactly how, step by step. This is the written companion to my YouTube episode (React from Scratch #6, in Moroccan Darija): https://youtu.be/a0qK0ezaQ0w
1. Install the router
React on its own doesn't know about pages. It knows components. For navigation between pages, you need an external library:
npm install react-router-dom After installing, check your package.json — the library should be listed there. Run the project again and confirm everything still works with no errors before moving on.
2. Define the routes
First, create one component per page, each in its own file — Home.jsx, Services.jsx, About.jsx, Contact.jsx — the same way I organized components in the previous episode.
Then wrap the whole app in BrowserRouter. It watches the URL and decides what to show:
import { BrowserRouter, Routes, Route } from 'react-router-dom'; import Home from './pages/Home'; import Services from './pages/Services'; import About from './pages/About'; import Contact from './pages/Contact'; function App() { return ( <BrowserRouter> <Routes> <Route path="/" element={<Home />} /> <Route path="/services" element={<Services />} /> <Route path="/about" element={<About />} /> <Route path="/contact" element={<Contact />} /> </Routes> </BrowserRouter> ); } Each Route connects a URL path to a page component. When the visitor goes to a path, the router renders the matching page — without reloading the whole site. Change the URL and watch the page swap in place.
3. Link vs a normal <a> — the beginner mistake I made
When I first built navigation, I used normal <a href> links. That's a big mistake inside a React app: every click makes the browser reload the entire page from the server. Everything unmounts and rebuilds from scratch. You lose all the speed React gives you.
The fix is the Link component that comes with the router:
import { Link } from 'react-router-dom'; <Link to="/services">Services</Link> It looks like a normal link, but clicking it never reloads the page. The router just swaps the visible component, instantly. And the browser back button works properly, because the router records every page you visited.
The rule is simple: inside a React app, always use Link for internal navigation. Keep <a> only for links that leave your site.
4. One shared Layout, no repetition
Now the header and footer need to appear on every page. Copying them into each page would repeat code — exactly what we learned to avoid.
The answer is a Layout component: header on top, footer at the bottom, and an empty slot in the middle called Outlet:
import { Outlet } from 'react-router-dom'; function Layout() { return ( <> <header>{/* navigation */}</header> <main> <Outlet /> </main> <footer>{/* footer */}</footer> </> ); } Then nest the four routes inside the Layout route in App.jsx. The router renders each page's content inside the Outlet, while the header and footer stay put. Change the header in one place, and it changes on every page.
5. Don't forget the 404 page
What happens when a visitor types a URL that doesn't exist? Without handling it, the router renders nothing — a blank page. That's not professional.
Add a catch-all route with path="*" pointing to a NotFound component: a big title saying the page doesn't exist, a short explanation, and a Link back to Home.
<Route path="*" element={<NotFound />} /> Try a wrong URL now: the 404 page appears instead of a blank screen.
The result
The site is now a real multi-page website: four pages, instant navigation, a shared layout, and a proper 404 page.
Your challenge: add the Contact page with a form (name, email, message) that shows a thank-you message when submitted. If you can do that on your own, you're starting to understand how real React websites are built.
I'm Wahib El Khadiri, a junior full-stack developer from Kénitra, Morocco. I build with React, Node.js and Laravel, contribute to open source, and teach React in Darija on YouTube. Portfolio: https://wahib-portfolio.netlify.app — GitHub: https://github.com/WAHIB-EL-KHADIRI

