Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.location.href vs redirect utility of react-router-dom

I have webapp written using React and React Router.

Are there any pros/cons of using window.location.href to redirect vs using the component provided via react-router?

like image 378
okapoor Avatar asked Feb 04 '23 22:02

okapoor


1 Answers

Use history.push('/page-name') if you don't want the browser window to be reloaded, or window.location.href if you want to reload it. Usually when using the React Router the default behavior is using the history.push (this.props.history.push('/page-name') to be more specific, as the rest of the navigation through the application is already using React Route's <Link>, which has the same effect.

like image 73
Rafael Cavalcante Avatar answered Mar 25 '23 07:03

Rafael Cavalcante