#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

What is Routing?

Routing is the system that decides what a user sees for a given URL. On a traditional website, each URL corresponds to a separate HTML file sent by the server. In a single page application built with React, the browser loads one HTML page, and a client-side router swaps components in and out as the URL changes, without asking the server for a new page.

Server routing versus client routing

AspectServer-side routingClient-side routing
What happens on a link clickBrowser requests a new page and reloadsJavaScript updates the URL and renders new components
Who decides the contentThe server, per requestThe router inside the app
Speed after first loadEvery page is a full round tripInstant view changes; only data is fetched
State between pagesLost on every reloadKept in memory, such as a cart or open menu

How a client-side router works

The browser's History API lets JavaScript change the address bar with history.pushState without reloading. A router library uses this in three steps. First, it intercepts clicks on its link components and calls the History API instead of letting the browser navigate. Second, it compares the new URL against a list of route definitions. Third, it renders the component that matches. It also listens for the Back and Forward buttons so they move between views as users expect.

import { BrowserRouter, Routes, Route, Link } from "react-router-dom";

function Home() { return <h2>Home</h2>; }
function About() { return <h2>About us</h2>; }
function NotFound() { return <h2>Page not found</h2>; }

export default function App() {
  return (
    <BrowserRouter>
      <nav>
        <Link to="/">Home</Link> | <Link to="/about">About</Link>
      </nav>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
        <Route path="*" element={<NotFound />} />
      </Routes>
    </BrowserRouter>
  );
}

Here BrowserRouter connects the app to the browser's URL, Routes picks the best match, and each Route pairs a path with the element to show. The * path catches anything unmatched.

What a good router gives you

  • Bookmarkable views: every screen has its own URL that can be shared or saved.
  • Working Back button: users move through history the way they do on any site.
  • Dynamic segments: one route can serve many items, like each product's detail page.
  • Nested layouts: a dashboard can keep its sidebar while the inner panel changes.
  • Code splitting: rarely visited pages can be loaded only when needed.

The full-reload mistake

The most common error when adding routing is using normal anchor tags for internal links: <a href="/about">. The page still reaches the right view, so it seems fine, but every click reloads the entire app, downloads the JavaScript again and throws away all state. Use the router's Link or NavLink components for internal navigation, and keep plain anchors for external sites.

A related surprise appears after deployment: refreshing a URL like /about can give a server 404, because the server has no file at that path. The host must be told to return index.html for unknown paths so the router can take over.

Routing without a library

You could listen to popstate events and switch components manually, and some tiny apps do. Libraries such as React Router handle the many edge cases, including nested routes, parameters, relative links and scroll behaviour, so they are the practical choice for real projects.

Takeaway: routing maps URLs to views; in a React app a client-side router uses the History API to change the URL and render the matching component without reloading, so always navigate with Link rather than plain anchors.

Ready to master real-world web development?

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

Explore Course