1

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.

Ajeet Shah
  • 18,551
  • 8
  • 57
  • 87
Kate
  • 288
  • 2
  • 4
  • 17

2 Answers2

2

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.

Ajeet Shah
  • 18,551
  • 8
  • 57
  • 87
0

If you want to make whole card clickable you also try -

<a href="www.example.com" target="_blank"><Card /></a>

Since you are trying to move out of the current application, so it does not matter if you are using react router or not.