Home > Software engineering >  How can i programmatically throw 404 error in react router?
How can i programmatically throw 404 error in react router?

Time:05-12

While using react-router for quite some time, I wonder can we do programmatically throw 404 page in react-router v5 ?

CodePudding user response:

You can use Redirect from react router. Rendering <Redirect> will navigate to a new location. The new location will override the current location in the history stack. For example:

<Route exact path="/">
  {loggedIn ? <Redirect to="/page_not_found" /> : <PublicHomePage />}
</Route>

CodePudding user response:

You can get to the 404 page in the following ways:

<Route path = "/*" element={} />

  • Related