#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
Routing and Navigation

Working with Links

In a React app that uses React Router, links let users move between routes without reloading the page. Instead of a plain <a href>, which makes the browser request a brand new document, you use the Link and NavLink components. They render a normal anchor tag, so they stay accessible and can be opened in a new tab, but on a regular click they update the URL and show the matching route instantly.

Link for ordinary navigation

Link takes a to prop with the path, much like href. Inside routes, paths without a leading slash are relative to the current route.

NavLink for menus that show the active page

NavLink works like Link but knows whether its path matches the current URL. It adds an active class by default, and its className and style props can be functions that receive { isActive }. It also sets aria-current="page" on the active link, which tells screen readers where the user is.

import { Link, NavLink, useNavigate } from 'react-router-dom';

export function MainNav() {
  return (
    <nav>
      <NavLink to="/" end className={({ isActive }) => (isActive ? 'nav active' : 'nav')}>
        Home
      </NavLink>
      <NavLink to="/products" className={({ isActive }) => (isActive ? 'nav active' : 'nav')}>
        Products
      </NavLink>
      <Link to="/cart">Cart</Link>
    </nav>
  );
}

export function ProductCard({ product }) {
  return (
    <article>
      <h3>{product.name}</h3>
      <Link to={`/products/${product.id}`}>View details</Link>
    </article>
  );
}

export function CheckoutButton({ cartIsEmpty }) {
  const navigate = useNavigate();
  function handleClick() {
    if (cartIsEmpty) return;
    navigate('/checkout');              // go there in code
  }
  return <button onClick={handleClick}>Checkout</button>;
}

The end prop on the Home link is important. Without it, / matches every path that begins with a slash, so Home would appear active on every page.

Navigating from code

Sometimes navigation happens after an action rather than a click on a link, such as after a successful login or form save. The useNavigate hook returns a function for that. navigate('/dashboard') goes forward, navigate(-1) goes back one step, and navigate('/login', { replace: true }) replaces the current history entry so the back button does not return to the page you just left. Use a link when the user is choosing where to go, and navigate when the program decides.

Useful Link props

PropWhat it does
toDestination path, string or object with pathname and search.
replaceReplaces the current history entry instead of adding one.
statePasses data to the next page, read there with useLocation().state.
end (NavLink)Active only on an exact match, not on child paths.

Mistakes with links

The most common one is using <a href="/products"> for internal pages. It still works, but it reloads the entire app, throws away all state, and downloads the bundle again. Use Link for routes inside the app, and keep plain <a> tags for external sites and file downloads.

The second is putting a Link outside the router. Links and hooks like useNavigate must be rendered inside BrowserRouter or a router created with createBrowserRouter; otherwise React throws an error saying they may only be used in the context of a router.

A third is using a <button> with navigate for something that is really a link. Buttons cannot be opened in a new tab or have their address copied, and screen readers announce them as actions. If it takes the user somewhere, make it a link.

In practice

Use Link for in-app navigation, NavLink for menus that highlight the current page, with end on the root link, and useNavigate for navigation triggered by code. Keep regular anchors for external URLs, and always render links inside the router.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course