#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

Why Routing?

Routing is what lets a single-page React app show different screens for different URLs. Without it, your whole app lives at one address, so users cannot bookmark a page, share a link to a product, use the back button sensibly or open something in a new tab. Client-side routing gives you those familiar browser behaviours while keeping the fast, no-reload feel of a single-page app.

The problem with one URL for everything

A simple way to switch screens is to store the current page in state and render a component based on it: {page === 'cart' ? <Cart /> : <Home />}. It works for a demo, but the address bar never changes. That causes real problems:

  • Refreshing the page always drops the user back on the home screen.
  • The back button leaves the app entirely instead of returning to the previous screen.
  • Links cannot point to a particular screen, so sharing and bookmarking are impossible.
  • Search engines and analytics see only one page.

What a router gives you

A routing library such as React Router keeps the URL and the screen in sync. When the user clicks a link, it updates the address with the browser's History API and renders the matching component. When the user presses back or forward, it listens for the change and renders the right screen again, all without asking the server for a new HTML page.

FeatureBenefit to users
URLs per screenBookmarks, shareable links, new tabs
History integrationBack and forward buttons behave as expected
URL parametersOne component can show any product, like /products/:id
Nested layoutsHeaders and sidebars stay while inner content changes
Protected routesRedirect signed-out users away from private pages
Code splitting by routeLoad each screen's code only when it is visited

Client-side versus server-side routing

On a traditional website, every link sends a request to the server, which returns a whole new HTML page. The browser clears the old page, loses any JavaScript state and draws everything again. In client-side routing, the server sends one HTML shell and the JavaScript bundle once; after that, the router swaps components in the browser. Transitions feel instant, and state such as a half-filled form in a sidebar or an open audio player survives navigation.

// Without routing: state decides the screen, URL never changes
function App() {
  const [page, setPage] = useState('home');
  return (
    <>
      <button onClick={() => setPage('home')}>Home</button>
      <button onClick={() => setPage('cart')}>Cart</button>
      {page === 'home' ? <Home /> : <Cart />}
    </>
  );
}

// With React Router: the URL decides the screen
// /       -> <Home />
// /cart   -> <Cart />
// <Link to="/cart">Cart</Link> updates the address bar and the view

Trade-offs to keep in mind

Client-side routing has costs. The first load downloads more JavaScript before anything appears, which is why route-based code splitting helps. Content rendered only in the browser can be harder for some crawlers to read, which is one reason frameworks built on React add server rendering.

There is also a classic deployment trap. Since the server only knows about index.html, a refresh on /cart asks the server for a file that does not exist and returns a 404. The server needs a fallback rule that serves index.html for every app route, so the router can take over.

Finally, routing is not a security layer. Hiding a route in the browser does not protect data; the API must still check whether the user is allowed to see it.

The short answer

You need routing when your app has more than one screen that users might want to reach directly. It ties screens to URLs so links, refreshes and the back button all behave the way people expect from the web.

Ready to master real-world web development?

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

Explore Course