admin管理员组

文章数量:1421483

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?

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?

Share Improve this question asked Feb 13, 2021 at 12:26 thegreatjedithegreatjedi 3,0485 gold badges34 silver badges56 bronze badges 1
  • 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 Commented Feb 13, 2021 at 12:30
Add a ment  | 

1 Answer 1

Reset to default 6

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.

本文标签: javascriptWhen should I use Link over NavLinkStack Overflow