Handling 404 Pages and Redirects

Tutorial 5 of 5

1. Introduction

Goal of the tutorial: This tutorial aims to teach you how to manage 404 pages and implement redirects in a React application.

Learning outcomes: By the end of this tutorial, you'll be able to provide an appropriate user feedback when a page isn't found and direct users to the correct content using redirects.

Prerequisites: Basic understanding of React and JavaScript is required.

2. Step-by-Step Guide

404 is the HTTP status code for not found. This means the server could not find the requested URL. In a React application, we can handle 404s by using routing. When none of the routes match, we can return a 404 page.

Redirects are a way to send both users and search engines to a different URL from the one they originally requested. We can implement redirects in our React application using the Redirect component from react-router-dom.

Installing react-router-dom

Before we start, ensure that you have react-router-dom installed. If not, install it using:

npm install react-router-dom

Basic Route Setup

First, let's set up some basic routes. For this, we'll need the BrowserRouter and Route components from react-router-dom.

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

function App() {
  return (
    <Router>
      <Route path="/" exact component={HomePage} />
      <Route path="/about" component={AboutPage} />
      <Route path="/contact" component={ContactPage} />
    </Router>
  );
}

export default App;

3. Code Examples

Handling 404

To handle 404s, we can use the Switch component from react-router-dom. The Switch component will only render the first Route or Redirect that matches the location. So, if none of the routes match, we can return a 404 page.

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

function App() {
  return (
    <Router>
      <Switch>
        <Route path="/" exact component={HomePage} />
        <Route path="/about" component={AboutPage} />
        <Route path="/contact" component={ContactPage} />
        <Route component={NotFoundPage} />
      </Switch>
    </Router>
  );
}

export default App;

In the above code, if the URL doesn't match any of the first three routes, the NotFoundPage component will be rendered.

Implementing Redirects

We can implement redirects using the Redirect component from react-router-dom. For example, if we want to redirect from /old-path to /new-path, we can do:

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

function App() {
  return (
    <Router>
      <Switch>
        <Route path="/" exact component={HomePage} />
        <Route path="/about" component={AboutPage} />
        <Route path="/contact" component={ContactPage} />
        <Redirect from="/old-path" to="/new-path" />
        <Route component={NotFoundPage} />
      </Switch>
    </Router>
  );
}

export default App;

In the above code, if the URL is /old-path, it will redirect to /new-path.

4. Summary

In this tutorial, we have learned how to handle 404 pages and implement redirects in a React application. We used the react-router-dom library to achieve this.

5. Practice Exercises

  1. Create a simple React application with routes for Home, About, and Contact pages. Implement a 404 page that displays when the URL doesn't match any of these routes.

  2. Add a redirect from /old-about to /about. Test this by visiting /old-about and verifying that you are redirected to /about.

Solutions

  1. This is similar to the code example provided in the tutorial. You just need to create the HomePage, AboutPage, ContactPage, and NotFoundPage components and use them in the routes.

  2. This is also similar to the code example provided in the tutorial. You just need to add <Redirect from="/old-about" to="/about" /> to your routes.

Remember, practice makes perfect. Continue to experiment with different routes and redirects. Happy coding!