Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore react-router route

There's a component on '/example' route. When I navigate to that path, the component being rendered. The problem is that I need to ignore rendering component and get redirected to the external link (which happen to have the same route) - '/example'. Is there any way for me to ignore or get around react-router route?

like image 481
nevs nevs Avatar asked Oct 25 '25 05:10

nevs nevs


2 Answers

Here's a one-liner for using React Router to redirect to an external link:

<Route path='/privacy-policy' component={() => { 
     window.location.href = 'https://example.com/1234'; 
     return null;
}}/>
like image 74
Trần Công Avatar answered Oct 26 '25 18:10

Trần Công


You can try using Redirect

<Route path="/example" render={() => {
   return <Redirect  to="//external.url/example" />
}} />

Or if you have a <Switch> around the routes you can shorten it:

<Redirect from="/example" to="//external.url/example" />
like image 36
Carlos Sá Avatar answered Oct 26 '25 18:10

Carlos Sá