π11. React Routing in Detail (with Simple Examples)


π Introduction:
When you create websites with multiple pages β like Home Page, About Page, Contact Page β
then React Router helps you move between pages without reloading the whole website.
π React Router makes your React app work like a real website with smooth page navigation.
π₯ 1. Installing react-router-dom
To use Routing in React, first you need to install a package called react-router-dom
.
This package helps us to create multiple pages inside React app.
π Open your terminal and run:
npm install react-router-dom
π₯ 2. Basic Concepts of React Router
Term | Meaning |
BrowserRouter | It tells React to use the Router in your app. |
Routes | It contains all your <Route> components. |
Route | It shows the correct page/component based on the URL. |
Link | It helps you to move between pages (like an anchor <a> tag, but without page reload). |
useNavigate | It is a hook to move the user to another page programmatically. |
useParams | It is a hook to access dynamic values from the URL. |
π₯ 3. How to Set Up Routing
Step-by-Step Example π
π /src/App.js
import React from 'react';
import { BrowserRouter, Routes, Route } from 'react-router-dom';
import Home from './pages/Home';
import About from './pages/About';
import Contact from './pages/Contact';
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/contact" element={<Contact />} />
</Routes>
</BrowserRouter>
);
}
export default App;
π Create Pages
In /src/pages
folder, create these 3 files:
Home.js
import React from 'react';
import { Link } from 'react-router-dom';
function Home() {
return (
<div>
<h1>Home Page π </h1>
<Link to="/about">Go to About Page</Link><br/>
<Link to="/contact">Go to Contact Page</Link>
</div>
);
}
export default Home;
About.js
import React from 'react';
import { Link } from 'react-router-dom';
function About() {
return (
<div>
<h1>About Us π</h1>
<Link to="/">Back to Home</Link>
</div>
);
}
export default About;
Contact.js
import React from 'react';
import { Link } from 'react-router-dom';
function Contact() {
return (
<div>
<h1>Contact Us π</h1>
<Link to="/">Back to Home</Link>
</div>
);
}
export default Contact;
π₯ 4. Important Points About Each Part
β BrowserRouter
Wrap your entire app inside
<BrowserRouter>
.It manages the history of the URLs internally.
β Routes and Route
<Routes>
is like a container.Inside it,
<Route>
defines "which component will open at which URL".
Example:
<Route path="/about" element={<About />} />
π means when user types /about
, the About component will show.
β Link
Instead of using normal
<a>
tags, use<Link>
.It moves to another page without refreshing the app.
Example:
<Link to="/contact">Contact Us</Link>
β useNavigate
- When you want to navigate (move) programmatically (on button click, after form submit etc.), use
useNavigate
.
Example:
import { useNavigate } from 'react-router-dom';
function Home() {
const navigate = useNavigate();
function goToContact() {
navigate('/contact');
}
return (
<div>
<h1>Home Page</h1>
<button onClick={goToContact}>Go to Contact Page</button>
</div>
);
}
β useParams
- If your URL has dynamic values (like product id, user id), use
useParams
to read that value.
Example: URL β /product/5
(here 5 is dynamic id)
Routing setup:
<Route path="/product/:id" element={<Product />} />
Reading id
inside Product.js:
import { useParams } from 'react-router-dom';
function Product() {
const { id } = useParams();
return (
<div>
<h1>Product ID: {id}</h1>
</div>
);
}
export default Product;
π¦ Real Life Example of React Router:
E-commerce websites: Home Page, Products Page, Product Details Page
Blog websites: Home, Articles, Single Article page
Portfolio websites: Home, About, Projects, Contact
π All these pages move smoothly without reloading β thanks to React Router!
π― Final Summary
Topic | What it Does |
BrowserRouter | Creates the Router environment |
Routes | Holds all the Route components |
Route | Defines which component for which URL |
Link | Moves between pages without reload |
useNavigate | Programmatically move user to other page |
useParams | Read dynamic URL values |
β¨ Bonus Tip:
If your app is big, keep Navbar outside <Routes>
, so that Navbar is always visible and only page content changes.
Example:
<BrowserRouter>
<Navbar />
<Routes>
...
</Routes>
</BrowserRouter>
π₯ Quick Code Recap (Diagram)
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/contact" element={<Contact />} />
<Route path="/product/:id" element={<Product />} />
</Routes>
</BrowserRouter>
π Conclusion
β
React Router helps you create multi-page apps easily.
β
Navigation becomes smooth and fast without reloading the page.
β
You can manage simple and complex websites easily using Routes, Link, Navigate and Params.
π React Router β FAQ (Frequently Asked Questions)
Q1. Is there any alternative to React Router in React applications?
Answer:
Yes, while react-router-dom
is the most widely used and official routing library for React, there are a few alternatives such as:
Wouter β A minimalist alternative that is lightweight and simpler.
Next.js Routing β If you are using Next.js, it comes with built-in file-based routing.
Reach Router β Previously used, but now merged with React Router (v6).
However, for most React projects, especially production-grade ones, React Router remains the most robust and feature-rich solution.
Q2. What is the biggest benefit of using React Router?
Answer:
The most significant benefit of using React Router is that it enables client-side routing. This means:
Pages can switch without reloading the entire website.
It improves performance and creates a smooth user experience.
It allows complex navigation like nested routes, dynamic URLs, redirects, and protected routes β all within a single-page application (SPA).
React Router gives you full control over navigation, making your application feel faster and more professional.
Q3. Can large-scale, enterprise-level applications be managed using React Router?
Answer:
Absolutely. React Router is production-ready and widely used in large-scale applications by many companies. It supports:
Nested and dynamic routes
Route guards and private routes
Lazy loading (code splitting)
Programmatic navigation
Custom route rendering
With proper architecture and state management (like Redux or Context API), React Router can scale efficiently, making it fully suitable for enterprise-level applications.
π Stay Connected
If you found this article helpful and want to receive more such beginner-friendly and industry-relevant React notes, tutorials, and project ideas β
π© Subscribe to our newsletter by entering your email below.
And if you're someone who wants to prepare for tech interviews while having a little fun and entertainment,
π₯ Donβt forget to subscribe to my YouTube channel β Knowledge Factory 22 β for regular content on tech concepts, career tips, and coding insights!
Stay curious. Keep building. π
Subscribe to my newsletter
Read articles from Payal Porwal directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Payal Porwal
Payal Porwal
Hi there, tech enthusiasts! I'm a passionate Software Developer driven by a love for continuous learning and innovation. I thrive on exploring new tools and technologies, pushing boundaries, and finding creative solutions to complex problems. What You'll Find Here On my Hashnode blog, I share: π In-depth explorations of emerging technologies π‘ Practical tutorials and how-to guides π§Insights on software development best practices πReviews of the latest tools and frameworks π‘ Personal experiences from real-world projects. Join me as we bridge imagination and implementation in the tech world. Whether you're a seasoned pro or just starting out, there's always something new to discover! Letβs connect and grow together! π