Skip to content
Advertisement

Why use history.push or some other method from react-router-dom instead of just assigning the path to window.location.pathname?

It seems easier to me when I need to navigate somewhere in my code to just assign the path where I want to go to window.location.pathname. Is there any disadvantage to doing this?

Advertisement

Answer

Because window.location.pathname triggers a reload, defeating the entire purpose of react-router, while history.push() just changes the url and doesn’t reload. Not reloading the entire page is one of the fundamentals of a single-page-application, SPA. https://reactrouter.com/web/api/history

Also see this: https://stackoverflow.com/a/43986829/12101554

Advertisement