Solved: react router reload page not found

The main problem related to React Router reload page not found is that when a user refreshes the page, the browser will attempt to make a request to the server for the current URL. However, since React Router is client-side routing, there is no corresponding server route for the URL and thus a 404 Not Found error is returned. This can be especially problematic if users are expecting certain content to appear on refresh.

import { BrowserRouter as Router, Route, Switch } from "react-router-dom";

const App = () => (
      <Route exact path="/" component={Home} />
      <Route path="/about" component={About} />

      {/* Page Not Found */}
      <Route render={() => (<h1>Page not found</h1>)} />


// Line 1: This line imports the BrowserRouter, Route, and Switch components from the react-router-dom library.

// Line 3: This line defines a function called App which returns JSX code.

// Line 5: This line wraps the entire JSX code in a Router component from react-router-dom.

// Line 6: This line wraps all of the routes in a Switch component from react-router-dom.

// Lines 7 – 9: These lines define two routes for ‘/’ and ‘/about’ respectively with their respective components (Home and About).

// Lines 11 – 12: These lines define a route for when no other routes match, which renders an h1 tag saying “Page not found”.

Page not found error

Page not found error in React Router is an error that occurs when a user attempts to access a page or route that does not exist. This can happen if the user has mistyped the URL, or if the page they are trying to access has been removed or moved. In React Router, this error is handled by using the component’s render prop and passing in a function that returns a 404 page. This allows developers to create custom 404 pages with whatever content they want, such as helpful links or an apology message.

How do I handle Page not found In react router

When using React Router, you can handle a page not found by creating a custom route that will render a NotFound component when the URL does not match any of the existing routes. To do this, you need to create a new route with the path set to “*” and render your NotFound component.

For example:

} />

This will match any URL that is not already matched by an existing route and render your NotFound component.

How do I force a reacting page to reload

In React Router, you can force a page to reload by using the replace method on the history object. This will replace the current entry in the history stack with a new one, thus forcing a reload of the page. To use this method, you need to first get access to the history object. You can do this by passing it as a prop when creating your router component:

const AppRouter = () => (

{/* Your routes here */}


Once you have access to the history object, you can use its replace method like so:


Related posts:

Leave a Comment