Skip to content

Sagar Kunwar

Menu
  • Home
  • YouTube
  • Blog
  • Contact
  • Privacy Policy
Menu

Navigating the Web with Style: Using React Router for Navigation

Posted on July 2, 2025July 7, 2025 by Sagar Kunwar

Hey there, fellow coder! If you’ve been dabbling with React and are ready to spice things up with some dynamic navigation, you’ve come to the right place. In today’s digital world, creating single-page applications (SPAs) with seamless page transitions is all the rage, and that’s where React Router comes into play.

In this tutorial, we’ll journey through setting up React Router in your project and explore its fundamental features. So, grab your virtual backpack and let’s embark on this coding expedition together!

Getting Started with React Router

What is React Router?

React Router is a standard library for routing in React. It enables your app to render the right components at the right times, based on your application’s URL.

Why should you use it?

  • Simplifies navigation between different components or pages within your application.
  • Keeps your UI and URL in sync.
  • Allows for nested and dynamic routing.

Setting Up React Router

Let’s start with a fresh React app:

npx create-react-app my-react-app
cd my-react-app

Now install React Router:

npm install react-router-dom

Creating a Basic Example

Let’s create three pages: Home, About, and Contact.

Home.js

import React from 'react';

function Home() {
  return <h2>Home Page</h2>;
}

export default Home;

About.js

import React from 'react';

function About() {
  return <h2>About Page</h2>;
}

export default About;

Contact.js

import React from 'react';

function Contact() {
  return <h2>Contact Page</h2>;
}

export default Contact;

Setting Up Routes in index.js or App.js

import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter, Route, Link, Switch } from 'react-router-dom';
import Home from './Home';
import About from './About';
import Contact from './Contact';

function App() {
  return (
    <BrowserRouter>
      <div>
        <nav>
          <ul>
            <li><Link to="/">Home</Link></li>
            <li><Link to="/about">About</Link></li>
            <li><Link to="/contact">Contact</Link></li>
          </ul>
        </nav>

        <Switch>
          <Route path="/about"><About /></Route>
          <Route path="/contact"><Contact /></Route>
          <Route path="/"><Home /></Route>
        </Switch>
      </div>
    </BrowserRouter>
  );
}

ReactDOM.render(<App />, document.getElementById('root'));

Styling the Navigation

Create a file called styles.css and add the following:

nav ul {
  list-style-type: none;
  padding: 0;
}

nav ul li {
  display: inline;
  margin-right: 20px;
}

nav ul li a {
  text-decoration: none;
  color: #007bff;
  font-weight: bold;
}

nav ul li a:hover {
  color: #0056b3;
}

Then import it in App.js:

import './styles.css';

Practicing React Router

Nice work! You’ve now built a basic routing setup with React Router. 🎉

Optional Practice Ideas

  • Dynamic Routing: Pass parameters via the URL and load user-specific pages.
  • Nested Routing: Create child routes inside parent routes.
  • Custom 404 Page: Add a wildcard <Route path="*"/> to handle unknown paths.

Wrapping Up

Look at you, cruising down the routing highway like a pro! You’ve now got the tools to navigate your React apps with style and purpose. From basic navigation to advanced features, React Router is your go-to for building seamless SPAs.

Keep exploring and happy coding! 💻✨

(Visited 8 times, 1 visits today)

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Understanding the DOM: A JavaScript Hands-On Walkthrough
  • Deploy Your Full-Stack App to Render in Under 10 Minutes
  • Beginner’s Guide: How to Use Firebase Realtime Database
  • Guide to Responsive Images with `srcset`

Categories

  • Blog
  • Javascript
  • PHP
  • Support
  • Uncategorized
  • Web Hosting
July 2025
S M T W T F S
 12345
6789101112
13141516171819
20212223242526
2728293031  
« Jun   Aug »
© 2026 Sagar Kunwar | Powered by Superbs Personal Blog theme