Search code examples
reactjsurlreact-routerredux-router

How to redirect to external url using .push redux-router?


I have Card which is redirecting on external URL on click. But instead of using location.href, I need to use redux-router's .push command. How can I do this?

Simple example of the code:

<Card 

return (
  title={props.title}
  onClick={() => {
    location.href='www.example.com'
  }}
)

If I use this props.navigate('www.example.com') it is returning the URL like a parameter on the localhost.


Solution

  • You cannot use Link or history.push from react-router to redirect to an external site. You can use window.location.href or window.location.replace.

    From history:

    We don't support redirecting out of domain period. In general you should handle redirects to external URLs separately.

    From Link:

    <Link/> is a router-aware anchor, if you want to link to an external site, use an <a/>.

    Edit:

    Redux-Router uses history and react-router packages internally, so external redirects would not be possible with it as well.