DEV Community

Saleh Mubashar
Saleh Mubashar

Posted on

4 1

Excluding certain file paths from in React Router

Hi guys.
In this post I will explain how to exclude certain file names or paths from react router.
For example, we want to redirect user to homepage if he types
any path except for some pages.



<Route
  render={({ location }) =>
    //This array includes pages on which user will
    // not be redirected
    ["/", "/register", "/dashboard", "/login"].includes(
      location.pathname
    ) ? null : (
      <Redirect to="/" />
    )
  }
/>


Enter fullscreen mode Exit fullscreen mode

If the pathname does not contain register, dashboard or login, it
will redirect to the homepage (/).


So that's about it.
If you have any questions, don't hesitate to ask
Check out my recent Hubpages tutorial:

Javascript Modal gallery
thanks :)

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more