DEV Community

Saleh Mubashar
Saleh Mubashar

Posted on

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)