Programming & Development / April 17, 2025

A Simple Guide to React Routing with React Router

React Router routing SPA navigation React routing example

In modern web applications, routing plays a key role in managing navigation and rendering components based on the URL. React Router is the most popular library for handling routing in React apps.

Let’s walk through a simple example to get you started with routing in React using react-router-dom.

πŸ“¦ Step 1: Install React Router

First, add the React Router dependency to your project:

bash

npm install react-router-dom

This will install everything needed to handle routing in your React app.

🧭 Step 2: Create Components

Let’s define a few components to simulate different pages in your application.

jsx

// Home.js
const Home = () => <h2>🏠 Home Page</h2>;

// About.js
const About = () => <h2>ℹ️ About Page</h2>;

// Contact.js
const Contact = () => <h2>πŸ“ž Contact Page</h2>;

Each of these components will be displayed based on the current URL path.

πŸ›  Step 3: Set Up Routing in App.js

Here's how to set up basic routing in your React app:

jsx

// App.js
import React from 'react';
import { BrowserRouter as Router, Route, Routes, Link } from 'react-router-dom';

const Home = () => <h2>🏠 Home Page</h2>;
const About = () => <h2>ℹ️ About Page</h2>;
const Contact = () => <h2>πŸ“ž Contact Page</h2>;

function App() {
  return (
    <Router>
      <div>
        <nav style={{ padding: '10px', backgroundColor: '#f0f0f0' }}>
          <Link to="/" style={{ margin: '0 10px' }}>Home</Link>
          <Link to="/about" style={{ margin: '0 10px' }}>About</Link>
          <Link to="/contact" style={{ margin: '0 10px' }}>Contact</Link>
        </nav>

        <Routes>
          <Route path="/" element={<Home />} />
          <Route path="/about" element={<About />} />
          <Route path="/contact" element={<Contact />} />
        </Routes>
      </div>
    </Router>
  );
}

export default App;

πŸ” How This Works

πŸ”— <Router>

Wraps your app and enables routing.

πŸ”— <Link>

Acts like an anchor tag (<a>) but prevents full page reload. Used for in-app navigation.

πŸ”— <Routes> and <Route>

  • <Routes> replaces the old <Switch> (React Router v6+).
  • <Route> maps a URL path to a specific component using element={<Component />}.

πŸš€ Step 4: Run the App

Start the development server:

bash

npm start

Now visit:

  • / β†’ Home
  • /about β†’ About
  • /contact β†’ Contact

And use the nav links to switch between themβ€”all without refreshing the page.

πŸ“ Summary

With just a few lines of code, you’ve set up React Router in your application:

βœ… Installed react-router-dom

βœ… Created page components

βœ… Configured routes using <Routes> and <Route>

βœ… Linked pages with <Link>

πŸ”§ Bonus Tips

  • For 404 pages, use:
jsx

<Route path="*" element={<NotFound />} />
  • For nested routes or layouts, you can combine <Outlet /> with parent routes.
  • For route parameters, use useParams() from react-router-dom.

React Router makes it easy to build single-page applications that feel seamless and dynamic. Whether you’re building a simple site or a large web app, it’s an essential tool in the React ecosystem


Comments

No comments yet

Add a new Comment

NUHMAN.COM

Information Technology website for Programming & Development, Web Design & UX/UI, Startups & Innovation, Gadgets & Consumer Tech, Cloud Computing & Enterprise Tech, Cybersecurity, Artificial Intelligence (AI) & Machine Learning (ML), Gaming Technology, Mobile Development, Tech News & Trends, Open Source & Linux, Data Science & Analytics

Categories

Tags

©{" "} Nuhmans.com . All Rights Reserved. Designed by{" "} HTML Codex