Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to implement hard 404 in ReactJS app

Introduction

Hello there, fellow programmers! If you are reading this, chances are that you have started your journey into the world of programming and are currently exploring ReactJS. If that's the case, congratulations on your choice! ReactJS is a powerful JavaScript library for building user interfaces and it's extremely popular in today's tech industry.

In this blog post, we will discuss a very important concept in web development: the 404 error, and how to implement it in a ReactJS application. Don't worry if you're unfamiliar with these terms, as I will explain everything in a way that's easy to understand.

What is a 404 error?

Imagine you're on a treasure hunt. You have a map leading to the treasure, but when you reach the supposed location, there's no treasure. That's disappointing, right? This is exactly what happens when a web server can't find the requested webpage – it's like a promise unfulfilled. This situation is represented by the HTTP status code 404, often referred to as a "404 error".

Why should we handle 404 errors?

Just as it's crucial to let treasure hunters know when the treasure doesn't exist, it's equally important to tell users when a webpage doesn't exist. Leaving users with a generic browser error message can be frustrating and confusing. Implementing a 404 error page can help guide users back to functional parts of your website and improve the overall user experience.

Understanding ReactJS

Before we start implementing a 404 error page in ReactJS, it's important to understand the basic structure of a ReactJS application. ReactJS uses components as its building blocks. These components are reusable and can be nested inside other components, allowing for complex applications to be built from simple, reusable pieces.

Implementing a 404 error page in ReactJS

Now, let's get to the fun part - implementing a 404 error page in a ReactJS application. We would be using React Router for this purpose. React Router is a standard library for routing in React. It enables the navigation among views of various components in a React Application, allows changing the browser URL, and keeps the UI in sync with the URL.

Setting up React Router

Let's first set up React Router in our application. The first thing we need to do is to install it, and we can do this using npm (node package manager), which is a package manager for JavaScript. Run the following command in your terminal:

npm install react-router-dom

Once installed, we need to import it into our application. In your main App.js file, add the following lines:

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

In the above lines, we're importing three components from 'react-router-dom': BrowserRouter, Switch, and Route.

Creating the 404 error component

Next, let's create our 404 error component. This is a simple React component that will be displayed when no other routes match.

Here is a basic example:

function NoMatch() {
  return (
    <div>
      <h2>Oops! Page not found</h2>
      <p>We're sorry, but the page you were looking for doesn't exist.</p>
    </div>
  );
}

Setting up routes

Now, we need to set up our routes to display the 404 error component when no other routes match. We can do this using the Switch and Route components from react-router-dom.

function App() {
  return (
    <Router>
      <Switch>
        <Route exact path="/">
          {/* Your homepage component goes here */}
        </Route>
        <Route path="/about">
          {/* Your about page component goes here */}
        </Route>
        {/* Add as many routes as you need */}
        <Route>
          <NoMatch />
        </Route>
      </Switch>
    </Router>
  );
}

In the above code, we're wrapping our application in a Router component. Inside the Router, we use a Switch. The Switch component renders the first child Route or Redirect that matches the location. So, if none of the defined paths match, the last Route component (which has no path defined) will be rendered, showing our 404 error component.

Wrapping up

And voila! You have successfully implemented a 404 error page in your ReactJS application. Now, whenever a user navigates to a non-existent route in your application, they will be directed to your custom 404 error page, enhancing the user experience.

Keep in mind that while this example is quite simple, you can make your 404 error page as complex and interactive as you want. The important thing is that it should guide the users back to a working part of your website.

Remember, learning to program is like building a wall brick by brick. Each new concept, each new line of code, is another brick. And this blog post has hopefully added a few more bricks to your wall. Happy coding!