what is the difference between history .push and navlink in reactjs

Viewed 668

In Reactjs I want to go to etc page now I can do this by using history.push("/etc") and I can also use <navlink to="/etc"> now the point is when to use history.push and when to Navlivk because both are fulfilling my requirement.

2 Answers

I usually use:

  • history.push("/etc") when I have to programmatically go into that page (i.e. click a button and if some conditions are respected then call history.push).

  • navlink when I want to show a link to the page to allow user to go in that page.

With the navlink you can navigate to another "page" by wrapping for example a button and do the redirect when clicking. Mostly this is what you probably want to do. But in some cases, you want to navigate to another "page" programmatically. For example when something changes in your app that has nothing to do with clicking on a button or link.

So you can use history.push to change the URL programmatically without the need to click on a button or a link.

Related