React-Router-Dom-npm - Everything You Need to Know

React-Router-Dom-npm - Everything You Need to Know

11 Sep 2025
Beginner
2.41K Views
15 min read
Learn with an interactive course and practical hands-on labs

Free React JS Online Course with Certificate - Start Today

react-router-dom is a routing library for React applications that run in the browser. The react-router-dom library is important for React developers working on React Applications. It helps them with dynamic routing within web apps.

This React Tutorial will give a gist of the meaning of react-router-dom, react-router-dom example, and much more. 70% of front-end job listings now require React.js expertise. Don’t get left behind—join our Free React Course with Certificate and build in-demand skills!

Read More: Top 50 React Interview Question & Answers

What is react-router-dom?

react-router-dom is an standard library for handling routing in React applications. It enables navigating between different components or pages in a React app without reloading the page—creating a Single Page Application (SPA) experience.

react router dom

Why to Use react-router-dom ?

1. Single-Page Applications

  • One of the most favored tools for managing routes within SPAs is react-router-dom.
  • react-router-dom allows navigating between pages (components) without reloading the page.
  • This library helps developers create navigable and dynamic user interfaces with ease.

2. URL-Based Navigation

  • Provides a way to manipulate the browser's URL, which enhances the user experience and supports deep linking.

3. Code Organization

  • Helps organize code by splitting app into routes (pages or views).
  • Makes large apps more maintainable.

4. Powerful Features

  • It contains features like Route protection (authentication/authorization)Nested routes,Redirects,Lazy loading components for best performance.

5. Widely Adopted & Well Maintained

  • It's the official routing library from the React community.
  • Highly compatible with React best practices.

Core Concepts of React Router DOM

In order to fully grasp the code, you need to know the fundamental aspects of react-router-dom:

Routes (Dynamic Routing)

  • These are used specifically to determine the path that your app will use and choosewhich component to present according to a given URL.
  • Allows defining dynamic routes with URL parameters.
  
<Route path="/user/:id" element={<UserProfile />} />

Navigation

  • React Navigation allows users to move between different parts of the application without refreshing the page.
  • Link replaces anchor tags (<a>), so navigation is done without page reloads.
  
<Link to="/contact">Contact Us</Link>

Components

  • react-router-dom provides several components that help set up and manage routing in a React application.
ComponentPurpose
<BrowserRouter>Wraps the app and enables routing using the browser’s History API (for clean URLs like /home, /about).
<Routes>Replaces <Switch> (from older versions). It holds all <Route> definitions and renders the first matching route.
<Route>Defines a path and the element (component) to render when the path matches the URL.
<Link>Creates a navigation link without reloading the page (like <a>, but SPA-friendly).
<Navigate>Redirects the user to another route programmatically. Useful for protected routes or after login/logout.

Using <BrowserRouter> and <Routes> to Set Up Routing

The first step in using react-router-dom is to set up the routing context with <BrowserRouter>. This component wraps your entire application and enables the use of the routing components.

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

function App() {
  return (
    <BrowserRouter>
      <Routes>
        {/* Define your routes here */}
      </Routes>
    </BrowserRouter>
  );
}

export default App;

Defining Routes with the <Route> Component

Within the <Routes> component, you define individual routes using the <Route> component. Each <Route> specifies a path and the component to render when the path is matched.
import Home from './Home';
import About from './About';

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
      </Routes>
    </BrowserRouter>
  );
}

Read More:

Creating Navigation Links with <Link>

To enable navigation between different routes, react-router-dom provides the <Link> component. This works similarly to an HTML <a> tag but without causing a page reload.
import { Link } from 'react-router-dom';

function Navbar() {
  return (
    <nav>
      <Link to="/">Home</Link>
      <Link to="/about">About</Link>
    </nav>
  );
}

Handling Dynamic URLs with Route Parameters

Sometimes, dynamic URLs, such as user profiles or product pages, need to be handled. This is where route parameters come in handy.
import { useParams } from 'react-router-dom';

function UserProfile() {
  const { userId } = useParams();
  return <div>User Profile: {userId}</div>;
}

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/user/:userId" element={<UserProfile />} />
      </Routes>
    </BrowserRouter>
  );
}

Nested Routing for Complex UIs in React Router DOM

For more complex applications, you might need nested routes. react-router-dom supports this by allowing you to nest <Route> components within other routes.
function Dashboard() {
  return (
    <div>
      <h2>Dashboard</h2>
      <Routes>
        <Route path="stats" element={<Stats />} />
        <Route path="settings" element={<Settings />} />
      </Routes>
    </div>
  );
}

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/dashboard/*" element={<Dashboard />} />
      </Routes>
    </BrowserRouter>
  );
}
Read More: React Developer Salary in India 2025 (For Freshers & Experienced)

Additional Features

React-router-dom also provides advanced features like redirects and route guards.

1. Redirects

Redirects are helpful when you need to navigate users from one route to another automatically. This can be helpful when you've updated your route structure but still want to support old URLs. The <Navigate> component is used for this purpose.
import { Navigate } from 'react-router-dom';

function App() {
  return (
    <BrowserRouter>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/old-path" element={<Navigate to="/new-path" />} />
        <Route path="/new-path" element={<NewComponent />} />
      </Routes>
    </BrowserRouter>
  );
}

2. Route Guards

We utilize route guards to limit access to particular routes based on certain conditions, such as customer verification. They guarantee that only authenticated consumers can access protected areas within apps.

3. Custom Route Handling with use routes

The useRoutes hook in React allows you to define your routes more declaratively, which can be useful for managing complex route configurations.

4. Programmatic Navigation with us navigate

Sometimes, you might need to navigate programmatically based on certain events, like form submissions. The useNavigate hook provides this capability.

5. Lazy Loading with React.lazy

Lazy loading in React allows you to load components only as required, making your application perform well. React, lazy, and Suspense make this possible.
Conclusion
react-router-dom is a powerful library used for dynamic routing in React applications. By understanding its core components and features, you can build dynamic SPAs easily.
React.js is powering apps like Netflix and Instagram. Don’t miss out—join our React JS Online Course and build world-class projects!

FAQs

react-router-dom is a library in React that helps in the dynamic routing within a web app.

You need to first, install the react-router and then, wrap your application with the BrowserRouter component. Now,use the Route component to define your routes and the Link component to easily navigate between them.

BrowserRouter sets the context for client-side routing and URL management while Route determines how particular components are displayed depending on the current URL or route.

React Router v6 uses hooks because of which class components like withRouter are deprecated in v6.

Take our React skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.

GET FREE CHALLENGE

Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at ScholarHat)

He is a renowned Speaker, Solution Architect, Mentor, and 10-time Microsoft MVP (2016–2025). With expertise in AI/ML, GenAI, System Design, Azure Cloud, .NET, Angular, React, Node.js, Microservices, DevOps, and Cross-Platform Mobile App Development, he bridges traditional frameworks with next-gen innovations.

He has trained 1 Lakh+ professionals across the globe, authored 45+ bestselling eBooks and 1000+ technical articles, and mentored 20+ free courses. As a corporate trainer for leading MNCs like IBM, Cognizant, and Dell, Shailendra continues to deliver world-class learning experiences through technology & AI.
Live Training - Book Free Demo
Azure AI Engineer Certification Training
20 Sep
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
Azure AI & Gen AI Engineer Certification Training Program
20 Sep
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
ASP.NET Core Certification Training
21 Sep
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
Advanced Full-Stack .NET Developer with Gen AI Certification Training
21 Sep
07:00AM - 09:00AM IST
Checkmark Icon
Get Job-Ready
Certification
Azure DevOps Certification Training
24 Sep
08:30PM - 10:30PM IST
Checkmark Icon
Get Job-Ready
Certification
Accept cookies & close this