Build a Custom 404 Not Found Page With React Router

In this lesson, we build the UI for the NotFound page shown when a user ever navigates to a route not defined in our app.

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL - Part Two course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL - Part Two, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course TinyHouse: A Fullstack React Masterclass with TypeScript and GraphQL - Part Two

Now, one component or basically one rendered page in a certain route of our app is the not found component. And it's the one component that we haven't provided any UI just yet. So in this moment of time, if I was to actually go to my application and try to access a route that doesn't really exist, I'm going to type a bunch of random characters here. We'll see them not found component, but we haven't set up any UI capability for it. So with that being said, let's quickly just set up some UI. We'll import a few things we'll need. We'll import the React library, which we've done already. We'll also import the fragment component that we're going to use very shortly. And we'll import three components from AMT design that will help us set up the UI for the not found component the way we expect it to. We'll import the empty components, the layout components, and the typography components. The empty component is incredibly straightforward and it's ant design capability to show an empty state placeholder. There's a few examples of different ways you can use it in the documentation, but we'll go with using it with one of the more basic simple ways. We'll further destruct the content components from layout. And we'll further destruct the text component from typography. In the not found component return statement, we'll return the content component with a class we've prepared before labeled not found. And within the content components, we'll render it the empty component from ant design. This empty component takes a description prop that can be used to describe the information being shown in the empty state placeholder. This is where we can actually provide just text or an actual react note. And we'll do this by wrapping what we're going to show here with react fragment components such that we don't have to use an additional development, where we're going to wrap separate elements together. We'll show the text component in our very first text component. We'll say, oh, something went wrong. And we'll provide a class here as well that we provided before. And this would be sort of the title we want to show. So we'll say not found description title. And then we'll show another text element. But in this particular case, we'll show a subtitle of sorts. And then here we'll just say the page you're looking for can't be found. If we were to go back to our client application, and now for the not found component, we see the empty state with the title and subtitle we prepared in not found pages or four or four pages is oftentimes useful to show a link to take the user somewhere in the application. So we can try and import the link components from react router DOM. And outside of our empty states, we can just simply attempt to show a link here . That will essentially just say go to home. We will provide the target route path to be the index routes. And I'll apply a class here as well that I've set up before. If we were to go back to our client application now, we'll see a link that says go to home. If we were to click it, it'll take us back to the index page. And something we've done before in the home page is we try to essentially style the link elements to buttons in certain cases. And the way we've done it to keep things simple is to use and design prepared class names and just throw it into the link component itself. And some of the class names you can apply is and button and button primary to get the primary appearance and and button large to get the large appearance of a button . Now if we were to take a look at our client application, we'll see the primary button that and design provides. But in this context, it's a link. And if I was to click it, it'll take us back to the index page. And as we've mentioned before, the capability to have a link via button can be done in many different ways. The more robust probable way that we would have done it is maybe create a button link component that essentially uses the button component from and design and perhaps uses the history object from React Router to help direct the user to assert a link in the page. But we're keeping things pretty simple here and just adding additional classes to make our link appears a button. And that's pretty much it. We've wanted our not found component to be fairly straightforward and just be a simple UI element to tell the user something went wrong and essentially provide a link to take them to the home page where they're able to navigate to. [BLANK_AUDIO]