0

I'm looking to have a redirect for every link that aren't specified in the configuration of my react app. The small difference with other tutorial is that I want to redirect to an html page which is present in my public folder and not a react component.

Something like this:

<Switch>
    <Route exact path="/" component={component}/>
    <Redirect  to="/404.html"/>
</Switch>

but it creates an infinite loop if I enter anything else than mysite/ and mysite/404.html. If i type mysite/aaaaaaaaaaaaaaaaa

it returns the following error : Maximum update depth exceeded

Any idea why it happens and how I can solve it ?

J. Lev
  • 197
  • 1
  • 15

2 Answers2

1

You should use a route with no path specified like that:

<Route component={NotFound} />

and create a NotFound component that renders a not found message or redirect to an external page if you prefer

Emanuele Scarabattoli
  • 2,273
  • 1
  • 5
  • 13
1

You can’t just point to an external link with React Router. The library defaults to route navigation within your app.

This has your answer:

React-Router External link

Craig Gehring
  • 1,509
  • 6
  • 10