0

With reference to React Router v4 <NavLink> vs <Link> benefits and the official documentation, NavLink is a variant of Link that allows you to apply styling attributes when it is the active link. These styling attributes do not appear to be mandatory, and I haven't found anything describing the pros of using Link.

If that's the case, why not just use NavLink everywhere? Are there any reasons to choosing Link over NavLink? Functionality perhaps, or better performance?

thegreatjedi
  • 2,438
  • 3
  • 18
  • 37
  • cause you might just need a link backing to the home page you don't need to apply any active class on it so u just make it a ```Link```, not a ```NavLink```. this is the main difference as I thought. – Abdelmonaem Shahat Feb 13 '21 at 12:30

1 Answers1

1

Well actually, the main difference between these two's is a class attribute. When we use the NavLink as a tag, it automatically inherit an active class when clicked. On the other hand, the Link tag does now have an active class when clicked.

When should I use the NavLink?

Just as the name implies 'NavLink', we use it mostly on navigation bars. This is because the active class permits us to define our custom styling in the App.css stylesheet. As such, we can use it to style our active buttons which in notify the use on which page he/she is currently on.

When should I use the Link?

The Link tag can be used where we want to do just some routing with no special effect. For instance; we can use the Link tag for scroll-to-top button, add to card buttons, submit button and more.

Akhil
  • 173
  • 8
  • So, is there any harm to just using `NavLink` for everything? For example, if I'm currently using a `Link`, and I replace it with a `NavLink` now, but chooses not to do anything when it gets the active class, then it's effectively the same thing, is that correct? – thegreatjedi Feb 14 '21 at 02:20
  • both do the job same i think there is no harm , the only diffrence is Link component is used to navigate the different routes on the site. But NavLink is used to add the style attributes to the active routes. – Akhil Feb 15 '21 at 03:52