DEV Community

Cover image for Navigating with React Router React Js Part 4: A Guide to Routing in React Applications
KyDev
KyDev

Posted on

Navigating with React Router React Js Part 4: A Guide to Routing in React Applications

Welcome back to our React series! In previous posts, we covered essential concepts such as components, state, props, and event handling. Now, it’s time to explore routing in React applications using React Router. Routing allows you to navigate between different views or components within your app, creating a seamless user experience 🚀.

What is React Router?

React Router is a powerful library that enables routing in React applications. It allows you to define multiple routes in your application and render specific components based on the URL path. This capability is crucial for building single-page applications (SPAs) where navigation does not require a full page reload.

Installing React Router
To get started, you’ll need to install React Router. You can do this using npm:

npm install react-router-dom
Enter fullscreen mode Exit fullscreen mode

Setting Up Basic Routing

Let’s set up a simple application with multiple routes. We will create an application with a home page, an about page, and a contact page.

1. Create Basic Components
First, create three components: Home, About, and Contact.

// Home.js
import React from 'react';

const Home = () => {
    return <h1>Home Page</h1>;
};

export default Home;

// About.js
import React from 'react';

const About = () => {
    return <h1>About Page</h1>;
};

export default About;

// Contact.js
import React from 'react';

const Contact = () => {
    return <h1>Contact Page</h1>;
};

export default Contact;
Enter fullscreen mode Exit fullscreen mode

2. Set Up the Router
Now, let’s set up the router in your main application file, typically App.js.

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

const App = () => {
    return (
        <Router>
            <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="/" exact component={Home} />
                <Route path="/about" component={About} />
                <Route path="/contact" component={Contact} />
            </Switch>
        </Router>
    );
};

export default App;
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Router: The BrowserRouter component wraps the entire application, enabling routing.
  • Link: The Link component is used to create navigation links that change the URL without reloading the page.
  • Route: The Route component defines a path and the component to render when the path matches.
  • Switch: The Switch component ensures that only one route is rendered at a time.

Navigating Between Pages
With the setup complete, you can now navigate between the Home, About, and Contact pages by clicking the links in the navigation menu. React Router will handle the URL changes and render the appropriate component without refreshing the page.

Route Parameters
You can also define routes with parameters, allowing you to pass dynamic data. For example, let’s create a User component that displays user information based on the user ID in the URL.

1. Create the User Component
User.js:

import React from 'react';
import { useParams } from 'react-router-dom';

const User = () => {
    const { userId } = useParams();
    return <h1>User ID: {userId}</h1>;
};

export default User;
Enter fullscreen mode Exit fullscreen mode

2. Update the Router
Add a route for the User component in your App.js:

<Route path="/user/:userId" component={User} />
Enter fullscreen mode Exit fullscreen mode

Nested Routes

React Router also supports nested routes, which allow you to render child routes within a parent component. This is useful for building complex layouts.

Example of Nested Routes:

  1. Create a Dashboard component with nested routes:
// Dashboard.js
import React from 'react';
import { Route, Link, Switch } from 'react-router-dom';
import Settings from './Settings';
import Profile from './Profile';

const Dashboard = () => {
    return (
        <div>
            <h1>Dashboard</h1>
            <nav>
                <Link to="/dashboard/profile">Profile</Link>
                <Link to="/dashboard/settings">Settings</Link>
            </nav>
            <Switch>
                <Route path="/dashboard/profile" component={Profile} />
                <Route path="/dashboard/settings" component={Settings} />
            </Switch>
        </div>
    );
};

export default Dashboard;
Enter fullscreen mode Exit fullscreen mode
  1. Update your App.js to include the Dashboard route:
<Route path="/dashboard" component={Dashboard} />
Enter fullscreen mode Exit fullscreen mode

now, navigating to /dashboard/profile or /dashboard/settings will render the respective components within the Dashboard.


In this post, we explored how to implement routing in a React application using React Router. We covered setting up basic routing, navigating between pages, using route parameters, and creating nested routes.

With these concepts, you can create a structured navigation system for your React applications, enhancing the user experience and enabling dynamic content rendering.

In the next post, we’ll delve into using React Hooks, focusing on useEffect and how it can manage side effects in functional components. Stay tuned!

Top comments (4)

Collapse
 
programmerraja profile image
Boopathi

This is a great introduction to React Router! I especially appreciate the clear explanations of setting up basic routes and how to use route parameters. Looking forward to the next post on React Hooks!

Collapse
 
kyydev profile image
KyDev • Edited

Thanks for the feedback! I’m really glad you found the explanations helpful. Stay tuned for the next post on React Hooks! If you have any questions, let’s discuss them together!

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
arindam_1729 profile image
Arindam Majumder

Why Have you tagged me?