How to catch 404 error in ReactJS
Getting Started with Error Handling in ReactJS
Imagine you're at a party. You're trying to find your best friend, who you were told was definitely attending. You're searching every room, but to no avail. What would you do? Probably ask around or call them, right? This is similar to how a web browser works when it can't find the page (or friend) it's looking for. It throws a 404 error, the equivalent of an "I can't find this" in the Internet world. In this blog post, we'll learn how to handle these 404 errors in ReactJS like a pro.
What is a 404 Error?
A 404 error is what the internet throws when it can't find a page. Think of it as the browser's way of saying: "I've looked everywhere, but I can't find what you're asking for". In technical terms, a 404 error is an HTTP status code signifying that the client could reach the server, but the server couldn't find what was requested.
Why is Handling 404 Errors Important?
Just like how it's rude to ignore someone's question at a party, it's considered bad practice to leave your users with a cold, impersonal error message when they hit a dead-end on your website. Handling 404 errors and presenting a friendly, informative error page can improve user experience significantly.
Handling 404 Errors in ReactJS
Now, let's dive into the code and see how to handle these errors elegantly in ReactJS.
React Router is a standard library for routing in React. It keeps your UI in sync with the URL. We'll use this library to demonstrate how to catch a 404 error.
First, we need to install React Router. Open your terminal, navigate to your project folder, and type the following command:
npm install react-router-dom
Setting Up Routes
After installing React Router, we want to set up routing for our application. Here's a very basic example:
import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Home from './Home';
import About from './About';
import Contact from './Contact';
import NotFound from './NotFound';
function App() {
return (
<Router>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
<Route component={NotFound} />
</Switch>
</Router>
);
}
export default App;
In the example above, Switch
is used to render only the first Route
or Redirect
that matches the location. If none of the Route
components match, then it will fall back to the NotFound
component, which we will use to display a friendly 404 error page.
Creating a Custom 404 Page
Now let's build our NotFound
component. It can be as simple or as complex as you want. The main goal here is to inform the user that the page they're looking for doesn't exist, and guide them back to a part of the site that does.
import React from 'react';
function NotFound() {
return (
<div>
<h1>404 - Not Found!</h1>
<p>Sorry, the page you are looking for does not exist.</p>
</div>
);
}
export default NotFound;
With this setup, when a user navigates to a page that doesn't exist, they are shown this NotFound
component.
Conclusion
We've all been at a party where we can't find the person we're looking for, and we've all felt that frustration when we can't find the webpage we need. It's essential to ensure your users never feel lost on your website. With the power of ReactJS and React Router, we can guide our users, ensuring they always find what they need or at least fall back to a safe place.
Happy coding! And remember, a party (or a website) is only as good as its host's (or developer's) ability to make their guests (or users) feel comfortable and informed.