1

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?

2
  • 5
    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. reactrouter.com/web/api/history Commented Nov 23, 2020 at 20:45
  • 1
    Thanks - I thought I had an issue in my authentication code that was causing those reloads. Just marked two issues off my list. :) Commented Nov 23, 2020 at 20:50

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

Comments

3

history.push doesn't cause your page to reload, window.location causes a page reload. The exact thing for <Link> and <a>

Comments

1

When you use history.push it redirects to the next page without reloading the entire page after adding it to the stack [array] of history and this feature is the main concept of the single-page applications

but window.location reload all of the page

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.