Routing with no page found(error 404) error handling

Sometimes user types a URL which does not exist in the website and the router fails and shows an error. To solve this problem we create a universal Route component which redirects to Link not found page whenever incorrect URL is passed

Approach:

  • Create 3 basic pages where you want to add navigation
  • Create a Navbar to handle navigation between the pages
  • Create NoPageFound.js file to handle routing for all incorrect routing
  • Add a path for non-configured routes which will be redirected to NoPageFound file
    the path with ‘*‘ handles all non-configured routes

Example: Add the following code in respective files after removing default styling in App.css

  • App.js: This file imports all the components and applies routing to them
  • AboutUs.js: This file contains the About Us Page
  • ContactUs.js: This file displays the Contact Us Page
  • Home.js: This component acts as the home page
  • Navbar.js: This component has the Navbar to redirect to all linking pages
  • NoPageFound.js: This page is displayed when invalid links are added.

Javascript




// App.js
  
import logo from "./logo.svg";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import "./App.css";
import NavBar from "./components/Navbar";
import Home from "./components/Home";
import AboutUs from "./components/AboutUs";
import ContactUs from "./components/CotactUs";
import NoPageFound from "./components/NoPageFound";
  
function App() {
    return (
        <div className="App">
            <BrowserRouter>
                <NavBar />
                <Routes>
                    <Route exact path="/" element={<Home />} />
                    <Route exact path="/about" element={<AboutUs />} />
                    <Route exact path="/contact" element={<ContactUs />} />
                    <Route path="*" element={<NoPageFound />} />
                </Routes>
            </BrowserRouter>
        </div>
    );
}
  
export default App;


Javascript




// AboutUs.js
import React from "react";
  
function AboutUs() {
    return (
        <div>
            <h2>w3wiki is a computer science portal for geeks!</h2>
            Read more about us at :
            <a href="https://www.w3wiki.org/about/">
                https://www.w3wiki.org/about/
            </a>
        </div>
    );
}
export default AboutUs;


Javascript




// ContactUs.js
import React from "react";
  
function ContactUs() {
    return (
        <address>
            You can find us here:
            <br />
            w3wiki
            <br />
            5th & 6th Floor, Royal Kapsons, A- 118, <br />
            Sector- 136, Noida, Uttar Pradesh (201305)
        </address>
    );
}
  
export default ContactUs;


Javascript




// Home.js
import React from "react";
  
function Home() {
    return <h1>Welcome to the world of Geeks!</h1>;
}
  
export default Home;


Javascript




// Navbar.js
  
import { Link } from "react-router-dom";
  
export default function NavBar() {
    return (
        <div>
            <ul className="r">
                <li>
                    <Link to="/">Home</Link>
                </li>
                <li>
                    <Link to="/about">About Us</Link>
                </li>
                <li>
                    <Link to="/contact">Contact Us</Link>
                </li>
            </ul>
        </div>
    );
}


Javascript




// NoPageFound.js
  
export default function NoPageFound() {
    return <h1>Error 404: Page Not Found</h1>;
}


Output:

Mastering React Routing: Learn Navigation and Routing in React Apps

In this article, we will learn how to implement Routing in React in an efficient way by implementing various aspects of routing such as dynamic routing, programmatically routing, and other concepts.

Table of Content

  • What is Navigation in React?
  • Routing with no page found(error 404) error handling
  • Programmatic Navigation in React
  • Dynamic Routing with React router

Similar Reads

What is Navigation in React?

...

Routing with no page found(error 404) error handling

Navigation means browsing between various parts of the application. In React mostly SPA(Single Page Application) are developed so Navigation is complex. We do not load new pages from the server side each time a navigation link is pressed instead routing is implemented on the client side....

Programmatic Navigation in React

Sometimes user types a URL which does not exist in the website and the router fails and shows an error. To solve this problem we create a universal Route component which redirects to Link not found page whenever incorrect URL is passed...

Dynamic Routing with React router

...