-2

is there any case where using just <Link /> is more suitable than <NavLink/>?

in other words, why does react-router provide a <Link /> component if there is a <NavLink/> there?

Thanks

  • 2
    Read the documentation, it tells you the difference: https://reactrouter.com/web/api/NavLink – Jayce444 May 26 '21 at 05:02
  • check this https://stackoverflow.com/questions/66185077/when-should-i-use-link-over-navlink – Riyana May 26 '21 at 05:05
  • @Jayce444 kindly read the question again, I'm not asking about the differences, I was wondering why not using NavLink for everything and customize according to our needs – Muhammed Yasser May 26 '21 at 05:19
  • Reading the documentation explains the differences, it's really up to you to make a decision which suits your needs more. They each serve *slightly* different purposes, so that is why the both exist. You keep saying you aren't asking about the differences, but the difference is ***exactly*** why they both exist. – Drew Reese May 26 '21 at 05:28
  • @MuhammedYasser the only reason is what the docs mention - that's it. Yes you could use NavLink and just not use the active link functionality, but if you don't need to why would you? NavLink is used for things like nav bars where the active one needs to be highlighted in some way. If you don't want to do that, and just want a plain old link, then that's what Link is for. As with lots of things, there's overlap, and you generally pick the one that **best** matches your use case. – Jayce444 May 26 '21 at 05:31
  • Because you are using React-router-dom you need to use Link or Navlink inside of it to be about to navigate, if you used a normal link you would be refreshing the entire app each time you click as you are navigating to a new page. – Richard Hpa May 26 '21 at 05:37

1 Answers1

0

I think they mention it properly on their doc:

A special version of the <Link> that will add styling attributes to the rendered element when it matches the current URL.

Al Amin
  • 479
  • 4
  • 11
  • I'm not asking about the differences, I was wondering why does Link component still exist if we can use NavLink without adding the styling attributes in the places of Link – Muhammed Yasser May 26 '21 at 05:15
  • Its pretty self explanatory isn't is? NavLink is mainly for links inside of your navigation and Link is for everywhere else? There isn't any point in using NavLink if it just redirects you to another page and you don't see the link you just clicked. – Richard Hpa May 26 '21 at 05:19
  • @RichardHpa "There isn't any point in using NavLink if it just redirects you to another page" aren't all links' job is to redirect us to another page? lol, if you mean redirect automatically then why would we use Navlink for this? I will just use vanillaJS or even a Redirect component – Muhammed Yasser May 26 '21 at 05:29