How to use .push in react-router to navigate to external url? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to use .push in react-router to navigate to external url?

I have container where i have card component. Simple code: <Card return ( title={props.title} onClick={()=>{ location.href='www.example.com' }} )/> So i need to change location.href and redirect to external link with react-router .push if i use props.navigate('www.example.com') its returning like property on localhost and redirect is not working. Can anyone help me?

5th Apr 2021, 2:25 PM
Kate Atanasoska
Kate Atanasoska - avatar
3 Answers
+ 1
I don't think you need to use .push for that. window.location.href = "url" would do just fine.
5th Apr 2021, 4:20 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 1
If I remember it correctly, all your components wrapped inside the <Route> component receives a history object as a prop. So may be props.history.push('path') shall work.
5th Apr 2021, 4:54 PM
Avinesh
Avinesh - avatar
0
It is hard for testing if i use window.location.hrev. Because this is simple example of the code, but the url is changing depending on the data.map. So it is required to use .push but i dont know how to use it
5th Apr 2021, 4:23 PM
Kate Atanasoska
Kate Atanasoska - avatar