4.7🚦 React Router – Navigating Between Pages in React 🧭

Table of Contents

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

React Router Basics
4.7🚦 React Router – Navigating Between Pages In React 🧭 2

πŸ–ΌοΈ A visual showing:

ConceptCodePurpose
<BrowserRouter>Wraps appEnables routing
<Route>Maps path to componentRenders component based on URL
<Routes>Groups all <Route>sOrganizes paths
<Link>Navigate between routesLike <a>, but no reload
useParams()Read :id from URLFor dynamic paths
useLocation()Read query stringsGet 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!

Let’s build the web β€” one route at a time! πŸš€

Share the Post:
Picture of Web Codder

Web Codder

Vikas Sankhla is a seasoned Full Stack Developer with over 7 years of experience in web development. He is the founder of Web Codder, a platform dedicated to providing comprehensive web development tutorials and resources. Vikas specializes in the MERN stack (MongoDB, Express.js, React.js, Node.js) and has been instrumental in mentoring aspiring developers through his online courses and content. His commitment to simplifying complex web technologies has made him a respected figure in the developer community.

Related Posts