How to setup 404 page in React Routing?
Last Updated :
26 Apr, 2025
When you're building a React app with routing, it's important to handle cases where users land on a page that doesn't exist. A 404 page is a good way to inform users that the page they’re looking for cannot be found.
Prerequisites:
Approach to Set Up a 404 Page in React Routing
Setting up a 404 page is crucial for improving user experience.
- First, create a PageNotFound component.
- Import this component into the app.js or where the routes are defined.
- Define routing for other pages first.
- In the last route component, use * as the URL path for the 404 page.
- 404 page.
Steps to Create a React Application
Step 1: Create a React application
Create a React application using the following command. After creating your project folder, move to the same folder.
npx create-react-app my-app
cd my-app
Step 2: Install React Router
Installing react-router-dom: react-router-dom can be installed via npm in your React application. Follow the steps given below to install react-router-dom in your React application: To install the react-router-dom use the following command:
npm install react-router-dom
Project Structure:
The updated dependencies in package.json file.
"dependencies": {
"@testing-library/dom": "^10.4.0",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^16.3.0",
"@testing-library/user-event": "^13.5.0",
"react": "^19.1.0",
"react-dom": "^19.1.0",
"react-router-dom": "^7.5.2",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
App.js
import React from "react";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Home from "./Home";
import About from "./About";
import PageNotFound from "./404Page";
function App() {
return (
<Router>
<div>
<h1>React Router Example</h1>
<nav>
<a href="/">Home</a> | <a href="/about">About</a>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="*" element={<PageNotFound />} /> {/* Catch-all route for 404 */}
</Routes>
</div>
</Router>
);
}
export default App;
About.js
import React from "react";
const About = () => {
return <h2>This is the About Page</h2>;
};
export default About;
404Page.js
import React from "react";
const PageNotFound = () => {
return (
<div>
<h2>404 Error</h2>
<p>Oops! The page you're looking for does not exist.</p>
</div>
);
};
export default PageNotFound;
Home.js
import React from "react";
const Home = () => {
return <h2>Welcome to the Home Page!</h2>;
};
export default Home;
Step 3: Test the 404 Page
Once everything is set up, it's time to test the 404 page. Try navigating to a non-existent route like:
http://localhost:3000/unknown
You should see the 404 error page appear, indicating that the route wasn’t found.
Output
Why You Should Create a 404 Page
- Improves User Experience: A custom 404 page lets users know they haven’t gone completely off-track and provides options to continue navigating the site.
- Prevents User Frustration: Instead of showing a generic error, a 404 page gives users a clear message and actionable steps, like a link to the homepage.
- Helps Retain Visitors: A good 404 page can guide users to other useful content, keeping them engaged instead of leaving your site.
- Supports Branding: You can use your brand’s colors, logo, and style to maintain a consistent look and feel, even on error pages.
Conclusion
Setting up a 404 page in React routing is simple and essential for a good user experience. By using the Switch and Route components in React Router, you can easily define routes for different pages and show a fallback 404 page when users land on an invalid route.
Similar Reads
How to setup 404 page in angular routing ?
To set up a 404 page in the angular routing, we have to first create a component to display whenever a 404 error occurred. In the following approach, we will create a simple angular component called PagenotfoundComponent. Creating Component: Run the below command to create pagenotfound component. n
2 min read
How to setup React Router v6 ?
React Router is an amazing library that was created by Remix. The purpose of React Router is to enable Client-Side routing in the React application, that is, by using React Router we can create different routes in our React app and can easily navigate to different pages without even reloading the pa
6 min read
How to implement client-side routing in React?
Client-side routing is a fundamental concept in modern single-page applications (SPAs) that allows for navigation within the application without a full page reload. In React, libraries like React Router provide the tools necessary to implement client-side routing seamlessly. In this article, we will
3 min read
How to Redirect to Another Page in ReactJS ?
In ReactJS, when you need to move users from one page to another, we can use the built-in React Router library, which is designed specifically for handling navigation within a React application.ApproachUse the Navigate component: In React Router v6, the Navigate component helps us to redirect users
6 min read
useRoutes Hook in React Router
React Router is a library that is used for handling navigation and routing in React applications. It provides a way to navigate between different components or pages while maintaining a single-page application (SPA) structure. One of the key features is the useRoutes hook. It allows you to define ro
4 min read
How to handle Nested Routes in React Router ?
React Router allows us to create a hierarchical structure where child components can be rendered within parent components, resulting in a seamless navigation flow. In this article, we will see how to handle nested Routes in React Router. Approach to handle Nested RoutesTo implement nested routes in
3 min read
How To Create A Multi-Page Website Using ReactJS?
Multi-page website using ReactJS is the core for building complex web applications that require multiple views or pages. Previously where each page reloads entirely, ReactJS allows you to navigate between different pages without reloading the whole page thanks to libraries like React Router.In this
4 min read
How to use the useHistory hook in React Router?
React Router is a standard library system built on top of React and used to create routing in the React application using the React Router Package. It enables the navigation between views of various components in a React Application. React Router is useful for Single Page Applications (SPAs). To use
4 min read
How To Route Programmatically In SvelteKit?
Routing is an important part of any web application, allowing users to navigate between different views or pages. SvelteKit, a modern framework for building web apps using Svelte, offers powerful routing capabilities both declaratively (through URL-based routes) and programmatically (using JavaScrip
4 min read
How to Integrate React Router with Remix?
Integrating React Router with Remix includes the usage of Remix's integrated router abilities thinking that Remix already uses React Router as its foundation. However, if you want to go beyond number one routing and leverage several React Router's extra precise or advanced skills.You may integrate t
7 min read