Handling Not Found Routes in React
Learn how to handle invalid URLs in React Router using wildcard routes. Create a custom 404 page, guide users back to valid pages, and improve navigation.
01 What Is a Not Found Route?
A Not Found Route handles a URL that does not match any
route defined in the application. For example, suppose
an application contains Home, About, and Courses pages.
If a user enters an invalid URL such as
/unknown-page, no normal route matches it.
Instead of leaving the user with an empty page or confusing content, we can display a custom 404 page explaining that the requested page could not be found.
02 Why Do We Need a 404 Page?
Users can accidentally enter an incorrect URL, follow an outdated link, or request a page that is not available. A custom Not Found page helps them understand the problem and continue using the application.
- It provides a clear message for invalid URLs.
- It prevents users from seeing an unexpected blank page.
- It can provide links back to valid pages.
- It improves the application's navigation experience.
- It keeps error handling consistent with the application's design.
404 - Page Not Found
Sorry, the page you are looking for does not exist.
Return to the Home page to continue browsing.
03 Understanding the Wildcard Route
React Router supports the wildcard path
*. It matches URLs that are not matched by
a more specific route in the current route configuration.
We can use this wildcard path to render a NotFound component. This component displays a message when a user visits an unknown URL.
<Route path="*" element={<NotFound />} />
Explanation:
-
path="*"defines a wildcard route. -
NotFoundis the component displayed for an unmatched URL. - Place the wildcard route alongside the other routes in the relevant Routes configuration.
04 Create the NotFound Component
First, create a component named NotFound. It will display the 404 heading, a helpful message, and a link that allows the user to return to the Home page.
import { Link } from "react-router-dom";
function NotFound() {
return (
<div>
<h1>404 - Page Not Found</h1>
<p>
Sorry, the page you are looking for
does not exist.
</p>
<Link to="/">Go to Home</Link>
</div>
);
}
export default NotFound;
Code Explanation:
-
NotFound()defines the custom error page component. - The heading communicates that the requested page was not found.
- The paragraph provides a helpful explanation to the user.
-
Linknavigates back to the application's Home route.
404 - Page Not Found
Sorry, the page you are looking for does not exist.
Go to Home
05 Configure the Not Found Route
Now define the normal application routes and add the wildcard route. In this example, the application contains Home, About, and Courses pages.
import {
BrowserRouter,
Routes,
Route
} from "react-router-dom";
import Home from "./Home";
import About from "./About";
import Courses from "./Courses";
import NotFound from "./NotFound";
function App() {
return (
<BrowserRouter>
<Routes>
<Route
path="/"
element={<Home />}
/>
<Route
path="/about"
element={<About />}
/>
<Route
path="/courses"
element={<Courses />}
/>
<Route
path="*"
element={<NotFound />}
/>
</Routes>
</BrowserRouter>
);
}
export default App;
How Does It Work?
- BrowserRouter: Enables browser-based routing.
- Routes: Groups the application's route definitions.
- Normal Routes: Define the Home, About, and Courses pages.
- Wildcard Route: Displays NotFound when no defined page route matches.
06 Test Different URLs
After configuring the routes, test valid and invalid URLs to verify that the correct component appears.
Home Page
Welcome to CIIT Training Institute.
About Page
Information about the application.
Courses Page
Available courses are displayed here.
404 - Page Not Found
Sorry, the page you are looking for does not exist.
Go to Home
07 Improve the Not Found Page
A Not Found page can provide more useful navigation options. For example, it can display the application name and links to the Home and Courses pages.
import { Link } from "react-router-dom";
function NotFound() {
return (
<div>
<h1>404 - Page Not Found</h1>
<p>
Welcome to CIIT Training Institute.
The requested page could not be found.
</p>
<nav>
<Link to="/">Home</Link>
{" | "}
<Link to="/courses">View Courses</Link>
</nav>
</div>
);
}
export default NotFound;
404 - Page Not Found
Welcome to CIIT Training Institute. The requested page could not be found.
Home | View Courses
The links use React Router's Link component so users can navigate to valid application routes without a full-page reload.
08 Common Mistakes to Avoid
- Forgetting to import the NotFound component.
- Defining the wildcard route outside the relevant Routes component.
- Using a URL that does not match the intended route path.
- Forgetting to provide a navigation link on the error page.
- Placing the wildcard fallback in the wrong nested route scope.
09 Practice Tasks
Complete these tasks to practise handling Not Found routes.
Task 1: Create a Custom 404 Page
Create a NotFound component that displays "404 - Page Not Found" and a helpful message.
Task 2: Add a Wildcard Route
Configure a wildcard route to display the NotFound component for unmatched URLs.
Task 3: Add Navigation Links
Add links to Home and Courses on the Not Found page.
Task 4: Test Invalid URLs
Visit URLs such as /wrong-page and /student-details and verify that the custom 404 page appears.
Task 5: Test Nested Routing
Create a dashboard with child routes and decide how an unknown URL under the dashboard should be handled.
Summary
Not Found Routes handle URLs that do not match the defined routes in a React application. React Router supports the wildcard path "*" to render a custom NotFound component. A useful 404 page explains the problem and provides links to valid pages. Testing both valid and invalid URLs helps verify the routing configuration and improves the user's navigation experience at CIIT Training Institute.