Building a Multi-Page Application with React Router

Expanding React Projects: Moving from Single to Multi-Page Applications with React Router

Jun 23, 2023·

4 min read

Building a Multi-Page Application with React Router

Introduction

In this blog post, we will dive into the topic of building a multi-page application using React Router. Whether you're a beginner or an experienced developer, this guide will provide you with step-by-step instructions and valuable insights to create dynamic and engaging web applications. So, buckle up and get ready to embark on this thrilling journey!

Setting the Stage

Before we jump into the nitty-gritty of React Router, let's take a moment to understand the importance of creating multi-page applications.

While single-page applications (SPAs) have gained popularity for their seamless user experience, there are cases where a multi-page architecture is more suitable. Multi-page applications provide clear navigation and allow us to organize our content logically across different pages.

With React Router, we can achieve the best of both worlds by combining the power of React's component-based architecture with traditional multi-page navigation.

Getting Started

To get started with building our multi-page application, we first need to set up a new React project. If you haven't installed Node.js and npm (Node Package Manager) yet, make sure to do so.

Once that's taken care of, open your terminal and run the following commands:

  1. Create a new React project:

     npx create-react-app my-app
     cd my-app
    
  2. Install React Router:

     npm install react-router-dom
    

With our project set up and React Router installed, we can now start building our multi-page application!

Creating Routes

In a multi-page application, we define routes that map specific URLs to different components. These components will be rendered when the corresponding route is accessed. Open the src/App.js file and let's start by importing the necessary modules:

import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';

Next, we can define our routes inside the App component:

function App() {
  return (
    <Router>
      <Switch>
        <Route exact path="/" component={Home} />
        <Route path="/about" component={About} />
        <Route path="/contact" component={Contact} />
        <Route component={NotFound} />
      </Switch>
    </Router>
  );
}

In the code above, we have defined four routes: the home page (/), the about page (/about), the contact page (/contact), and a catch-all route for any other undefined routes (NotFound component).

Creating Page Components

Now, let's create the components for our different pages. Create separate files for each page under the src directory:

  • Home.js

  • About.js

  • Contact.js

  • NotFound.js

In each of these files, we'll define a simple functional component that represents the content of the respective page. We'll cover these files in the next article.

Adding Navigation

A multi-page application won't be complete without navigation. Let's create a navigation menu that allows users to switch between different pages. Open the src/App.js file again, and inside the App component, add the following code:

import { Link } from 'react-router-dom';

// ...

function 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 components */}
      </Switch>


    </Router>
  );
}

With the above code, we have created a simple navigation menu using the Link component from React Router. The to prop specifies the URL path we want to navigate to when a menu item is clicked.

Resources

To further enhance your understanding of building multi-page applications with React Router, here are some recommended resources:

  1. Official React Router Documentation: link

  2. React Router Tutorial on React Training: link

  3. Medium article on React Router Best Practices: link

Conclusion

React Router is a powerful tool for building multi-page applications, combining the benefits of React's component-based architecture with traditional navigation. By creating routes, defining page components, and implementing a navigation menu, you can create dynamic and engaging web applications with ease. As you continue exploring this topic, remember to consult the recommended resources for in-depth understanding and best practices.


Now, I'd love to hear your thoughts and gather any questions or topics you'd like me to address in future articles related to anything React and Web Development. Your input will help me provide more valuable insights and cater to your specific interests.

Also, I tweet my blogs on Twitter. Make sure to follow me to get the latest updates regarding everything Web Development!

Did you find this article valuable?

Support Arjun by becoming a sponsor. Any amount is appreciated!