MyInternships.in

Routing with React Router

React Router Link and NavLink

Link renders an anchor that navigates without reloading the page. NavLink does the same but also tells you whether it matches the current URL, which is how you highlight the active navigation item.


What is Router Link and NavLink in React?

Link renders an anchor that navigates without reloading the page. NavLink does the same but also tells you whether it matches the current URL, which is how you highlight the active navigation item.

Why Router Link and NavLink matters

A plain anchor tag throws away the entire JavaScript application and reloads it. Link intercepts the click, updates history and re-renders only what changed.

Router Link and NavLink example

Navigation with active styling
JSX
import { Link, NavLink } from 'react-router-dom';

<Link to="/internships">Browse internships</Link>

<NavLink
  to="/internships"
  className={({ isActive }) => isActive ? 'nav active' : 'nav'}
>
  Internships
</NavLink>

Key points to remember

  • to accepts a string or an object with pathname, search and hash.
  • NavLink passes isActive and isPending to className and style callbacks.
  • Use the replace prop to swap the current history entry instead of pushing a new one.
  • Add end to NavLink so "/" does not stay active on every nested route.

Common mistakes with Router Link and NavLink

  • Mixing <a href="/page"> with Link in the same app, producing inconsistent full reloads.
  • Forgetting end on the home NavLink, so it appears active everywhere.

React Router Link and NavLink— Interview Questions & FAQs

What is the difference between Link and NavLink?+

They navigate identically. NavLink additionally exposes whether it matches the current location so you can style the active item — useful for navbars and sidebars.

Related React Topics

Keep learning with these closely related lessons.

Ready to use your React skills?

Find verified React internships and fresher developer jobs across India.

Browse React Internships