#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

Installing React Router

React Router is the most widely used library for client-side routing in React. Installing it adds components and hooks that map URLs to components, so /products shows a product list and /products/7 shows one product, all without full page reloads. Setting it up takes one package and a few lines that wrap your app in a router.

Installing the package

For a web project, install react-router-dom. In newer major versions the same APIs are also exported from the react-router package, but react-router-dom remains the familiar name in most tutorials and existing projects.

# inside a project created with Vite or similar
npm install react-router-dom

# check what got installed
npm ls react-router-dom

Check the major version in package.json. The examples here use the modern API from version 6 onward, which differs a lot from versions 5 and below.

Adding the router to your app

The recommended setup creates a router object with createBrowserRouter and passes it to RouterProvider at the root. Each route pairs a path with an element.

// main.jsx
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
import Home from './pages/Home';
import Products from './pages/Products';
import NotFound from './pages/NotFound';

const router = createBrowserRouter([
  { path: '/', element: <Home />, errorElement: <NotFound /> },
  { path: '/products', element: <Products /> },
]);

createRoot(document.getElementById('root')).render(
  <StrictMode>
    <RouterProvider router={router} />
  </StrictMode>
);

An alternative, often seen in older code and quick demos, wraps the app in <BrowserRouter> and defines routes with <Routes> and <Route> elements inside a component. Both work; the router-object style unlocks extra features such as loaders and actions for data.

Checking that it works

Run the dev server and type /products in the address bar. You should see the Products component. Then add a link with the router's Link component, such as <Link to="/products">Products</Link>, and click it; the URL changes and the new component appears without the browser reloading the page. You can confirm there was no reload because console logs and component state on unaffected parts survive the navigation.

Problems people hit after installing

The most frequent one is following a tutorial for a different version. Code using Switch, component={Home}, exact or useHistory belongs to version 5 and fails with errors like "export 'Switch' was not found". In current versions, use Routes or the router object, element={<Home />} and useNavigate.

Another is calling hooks such as useNavigate or useParams in a component that is rendered outside the router. React Router throws an error saying the hook may be used only in the context of a router. Make sure the provider wraps every component that uses router features.

A third shows up later: links work in development, but refreshing a deep URL like /products on the live site gives a 404. The server must be configured to return index.html for all app routes. Development servers like Vite do this automatically, which hides the problem until deployment.

Finally, avoid plain <a href> for internal navigation. It triggers a full reload and resets your app's state; use Link or NavLink.

Setup checklist

  1. Install react-router-dom and note the major version.
  2. Create a router and render RouterProvider at the root.
  3. Define routes with path and element.
  4. Navigate with Link, not plain anchors.
  5. Plan the server fallback before deploying.

Ready to master real-world web development?

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

Explore Course