React.js / React Routing and Navigation
Handling 404 Pages and Redirects
This tutorial will guide you through handling 404 pages and implementing redirects in your React application. You'll learn how to provide appropriate feedback when a page isn't fo…
Section overview
5 resourcesCovers implementing client-side routing and navigation with React Router.
1. Introduction
Goal of the tutorial: This tutorial aims to teach you how to manage 404 pages and implement redirects in a React application.
Learning outcomes: By the end of this tutorial, you'll be able to provide an appropriate user feedback when a page isn't found and direct users to the correct content using redirects.
Prerequisites: Basic understanding of React and JavaScript is required.
2. Step-by-Step Guide
404 is the HTTP status code for not found. This means the server could not find the requested URL. In a React application, we can handle 404s by using routing. When none of the routes match, we can return a 404 page.
Redirects are a way to send both users and search engines to a different URL from the one they originally requested. We can implement redirects in our React application using the Redirect component from react-router-dom.
Installing react-router-dom
Before we start, ensure that you have react-router-dom installed. If not, install it using:
npm install react-router-dom
Basic Route Setup
First, let's set up some basic routes. For this, we'll need the BrowserRouter and Route components from react-router-dom.
import React from 'react';
import { BrowserRouter as Router, Route } from 'react-router-dom';
function App() {
return (
<Router>
<Route path="/" exact component={HomePage} />
<Route path="/about" component={AboutPage} />
<Route path="/contact" component={ContactPage} />
</Router>
);
}
export default App;
3. Code Examples
Handling 404
To handle 404s, we can use the Switch component from react-router-dom. The Switch component will only render the first Route or Redirect that matches the location. So, if none of the routes match, we can return a 404 page.
import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
function App() {
return (
<Router>
<Switch>
<Route path="/" exact component={HomePage} />
<Route path="/about" component={AboutPage} />
<Route path="/contact" component={ContactPage} />
<Route component={NotFoundPage} />
</Switch>
</Router>
);
}
export default App;
In the above code, if the URL doesn't match any of the first three routes, the NotFoundPage component will be rendered.
Implementing Redirects
We can implement redirects using the Redirect component from react-router-dom. For example, if we want to redirect from /old-path to /new-path, we can do:
import React from 'react';
import { BrowserRouter as Router, Route, Redirect, Switch } from 'react-router-dom';
function App() {
return (
<Router>
<Switch>
<Route path="/" exact component={HomePage} />
<Route path="/about" component={AboutPage} />
<Route path="/contact" component={ContactPage} />
<Redirect from="/old-path" to="/new-path" />
<Route component={NotFoundPage} />
</Switch>
</Router>
);
}
export default App;
In the above code, if the URL is /old-path, it will redirect to /new-path.
4. Summary
In this tutorial, we have learned how to handle 404 pages and implement redirects in a React application. We used the react-router-dom library to achieve this.
5. Practice Exercises
-
Create a simple React application with routes for Home, About, and Contact pages. Implement a 404 page that displays when the URL doesn't match any of these routes.
-
Add a redirect from
/old-aboutto/about. Test this by visiting/old-aboutand verifying that you are redirected to/about.
Solutions
-
This is similar to the code example provided in the tutorial. You just need to create the
HomePage,AboutPage,ContactPage, andNotFoundPagecomponents and use them in the routes. -
This is also similar to the code example provided in the tutorial. You just need to add
<Redirect from="/old-about" to="/about" />to your routes.
Remember, practice makes perfect. Continue to experiment with different routes and redirects. Happy coding!
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Random Password Generator
Create secure, complex passwords with custom length and character options.
Use toolLatest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article