Hey buddy! π
Imagine you’re building a website, and you want users to move between pages like Home, About, and Contact β without the page reloading every time.
Thatβs where React Router comes in! π
React Router is like Google Maps for your app β it helps your users navigate without getting lost! πΊοΈ
Letβs break it all down in simple steps (like Iβm teaching you, my favorite curious cousin π).
π§ What is React Router?
React Router is a library that helps you:
- Switch between pages (or components)
- Stay on the same page (SPA = Single Page Application)
- Use nice URLs (like
/about
or/products/123
) - Keep everything fast and smooth π
π§ Setting Up React Router
Letβs install it first. Use npm or yarn:
bashCopyEditnpm install react-router-dom
Or if you prefer yarn:
bashCopyEdityarn add react-router-dom
Now, you’re ready to route like a pro! π§
π§± Basic Setup β Creating Pages in React
Letβs make 3 simple components:
jsxCopyEdit// Home.js
function Home() {
return <h2>π Welcome Home!</h2>;
}
// About.js
function About() {
return <h2>βΉοΈ About Us</h2>;
}
// Contact.js
function Contact() {
return <h2>π Contact Page</h2>;
}
ποΈ Adding Router in App.js
Now set up routing using BrowserRouter
, Routes
, and Route
:
jsxCopyEditimport React from "react";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import Home from "./Home";
import About from "./About";
import Contact from "./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;
π Boom! You now have a 3-page app without real page reloads!
π§ Navigation with <Link>
πͺ
Using normal <a>
tags reloads the page. β
Instead, use React Router’s <Link>
to move smoothly.
jsxCopyEditimport { Link } from "react-router-dom";
function Navbar() {
return (
<nav>
<Link to="/">π Home</Link> |
<Link to="/about">βΉοΈ About</Link> |
<Link to="/contact">π Contact</Link>
</nav>
);
}
π Now you can click and move around β super fast!
𧬠Dynamic Routing β Making URLs Flexible
Letβs say you have a list of products:
jsxCopyEdit<Link to="/product/1">Product 1</Link>
<Link to="/product/2">Product 2</Link>
Now letβs show details for each product using URL parameters:
jsxCopyEdit// Product.js
import { useParams } from "react-router-dom";
function Product() {
const { id } = useParams();
return <h2>π¦ Product ID: {id}</h2>;
}
And add the route in your app:
jsxCopyEdit<Route path="/product/:id" element={<Product />} />
π§ :id
is like a placeholder. It can be 1, 2, 99, anything!
π§ͺ Bonus: Use Query Strings
Query strings are like tiny notes in the URL.
Example: /search?term=react
Use useLocation
to read them:
jsxCopyEditimport { useLocation } from "react-router-dom";
function SearchPage() {
const location = useLocation();
const queryParams = new URLSearchParams(location.search);
const term = queryParams.get("term");
return <h2>π Search term: {term}</h2>;
}
π‘ URLs can now hold extra info without creating new routes!
π Infographic: React Router Basics

πΌοΈ A visual showing:
Concept | Code | Purpose |
---|---|---|
<BrowserRouter> | Wraps app | Enables routing |
<Route> | Maps path to component | Renders component based on URL |
<Routes> | Groups all <Route> s | Organizes paths |
<Link> | Navigate between routes | Like <a> , but no reload |
useParams() | Read :id from URL | For dynamic paths |
useLocation() | Read query strings | Get search data from URL |
β Best Practices for Using React Router
- π§Ό Keep URLs clean and meaningful
- π¦ Organize routes inside a
Routes
block - πΎ Use
useParams
for dynamic data - π§ Use
useNavigate
for programmatic navigation - π Protect private pages with authentication routes (coming soon!)
π Final Thoughts
React Router makes it super easy to build websites with multiple pages, smooth transitions, and friendly URLs β all without reloading. ποΈ
Start with basic routes and links, then explore dynamic paths and nested routes when you’re ready! πͺ
π¬ Keep Learning with Web Codder
Enjoyed this article? Want more React magic? πͺ
Hit that subscribe button and follow us everywhere!
- πΊ YouTube: Subscribe Now
- πΈ Instagram: Follow Us
- π¬ WhatsApp Community: Join Here
Letβs build the web β one route at a time! π