2-how-to-create-a-multi-page-app-using-react-and-nextjs.html

How to Create a Multi-Page App Using React and Next.js

In the ever-evolving landscape of web development, creating a robust and efficient multi-page application (MPA) is essential for delivering a rich user experience. React, a popular JavaScript library for building user interfaces, combined with Next.js, a powerful framework for server-rendered React applications, provides a seamless way to achieve this. In this article, we’ll explore how to create a multi-page app using React and Next.js, diving into definitions, use cases, and actionable insights.

Understanding Multi-Page Applications

What is a Multi-Page Application?

A multi-page application (MPA) is a web application that consists of multiple distinct pages, each loaded separately. Unlike single-page applications (SPAs), which dynamically update the content on a single page without refreshing, MPAs reload the entire page for each user interaction. This architecture can be beneficial for SEO, as each page can be indexed individually by search engines.

Use Cases for Multi-Page Applications

MPAs are ideal for:

  • Content-heavy Websites: Blogs, news sites, and portfolios where each page contains distinct content.
  • E-commerce Platforms: Where each product or category can be treated as a separate page.
  • Complex Applications: Where different functionalities require distinct routes and layouts.

Now that we have a grasp of what MPAs are, let’s dive into how to create one using React and Next.js.

Setting Up Your Development Environment

Before we start coding, ensure you have Node.js installed on your machine. If not, download it from the Node.js official website. Follow these steps to set up your Next.js project:

Step 1: Create a New Next.js Application

Open your terminal and run the following command:

npx create-next-app my-multi-page-app

This command creates a new directory called my-multi-page-app with all the necessary files and dependencies.

Step 2: Navigate to Your Project Directory

Change into your project directory:

cd my-multi-page-app

Step 3: Start the Development Server

Run your app locally:

npm run dev

You can now view your application by navigating to http://localhost:3000 in your web browser.

Creating Multi-Page Structure

Next.js simplifies the process of creating pages through its file-based routing system. Each file in the pages directory automatically becomes a route in your application.

Step 4: Create Your Pages

  1. Home Page: By default, Next.js creates an index.js file inside the pages directory. This will be your home page.

```javascript // pages/index.js import Link from 'next/link';

const Home = () => { return (

Welcome to My Multi-Page App

); };

export default Home; ```

  1. About Page: Create a new file named about.js in the pages directory.

```javascript // pages/about.js const About = () => { return (

About Us

This is the about page of our multi-page application.

); };

export default About; ```

  1. Contact Page: Similarly, create a contact.js file.

```javascript // pages/contact.js const Contact = () => { return (

Contact Us

This is the contact page. Reach out to us!

); };

export default Contact; ```

Step 5: Linking Between Pages

In your index.js file, you can now link to your newly created pages using the <Link> component from Next.js, as shown in the Home Page example.

Enhancing the Application

Now that we have a basic multi-page structure, let’s add some enhancements.

Step 6: Adding Navigation

You can create a reusable navigation component. Create a new folder called components and a file named Navbar.js:

// components/Navbar.js
import Link from 'next/link';

const Navbar = () => {
    return (
        <nav>
            <Link href="/">Home</Link>
            <Link href="/about">About</Link>
            <Link href="/contact">Contact</Link>
        </nav>
    );
};

export default Navbar;

Then, import and use this Navbar component in your index.js, about.js, and contact.js files.

Step 7: Adding Styles

Next.js supports CSS modules out of the box. You can create a styles folder and add CSS files to style your components. For example, create Navbar.module.css for your navigation styles:

/* styles/Navbar.module.css */
nav {
    background: #333;
    padding: 10px;
}

nav a {
    color: white;
    margin: 0 15px;
    text-decoration: none;
}

Then import this stylesheet in your Navbar.js:

import styles from '../styles/Navbar.module.css';

Troubleshooting Common Issues

As you build your multi-page app, you may encounter some common issues:

  • 404 Errors: Ensure that your file names in the pages directory are correct and match your intended routes.
  • Styling Issues: Double-check that your CSS files are imported correctly.
  • Linking Problems: Verify that the paths in your <Link> components are accurate.

Conclusion

Creating a multi-page application using React and Next.js is a straightforward process, thanks to Next.js’s file-based routing and built-in features. By following the steps outlined in this article, you can quickly set up a robust and efficient multi-page app that can grow with your needs.

With the knowledge gained here, you’re now equipped to build scalable, SEO-friendly applications that offer a smooth user experience. Embrace the power of React and Next.js, and take your web development skills to the next level!

SR
Syed
Rizwan

About the Author

Syed Rizwan is a Machine Learning Engineer with 5 years of experience in AI, IoT, and Industrial Automation.