React Router DOM is a powerful routing library for React applications that enables navigation and URL routing. In this article, we'll explore React Router DOM in-depth, covering its installation, basic usage, advanced features, and best practices.
What is React Router DOM?
React Router DOM is a collection of navigational components for React applications that allows developers to declaratively define the application's UI based on the URL. It provides a way to synchronize the UI with the URL, enabling client-side routing in single-page applications (SPAs).
Steps to Implement React-router-dom
Step 1: Create a React application using the following command:
npx create-react-app myapp
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd myapp
Project Structure:

Step 3: Next, install react-router-dom using the following command
npm install react-router-dom
The updated dependencies in package.json file will look like:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.22.3",
"react-scripts": "5.0.1",
},
Basic Usage
React Router DOM provides several components for defining routes in your application:
- BrowserRouter: Wraps your application with a router component that listens to changes in the URL.
- Router: Alias for BrowserRouter. (This is used for renaming convenience, if desired.)
- Routes: Container for defining routes in your application.
- Route: Defines a route that renders a component based on the current URL path.
Example:
JavaScript
// App.js
import {
BrowserRouter as Router,
Routes, Route
} from 'react-router-dom';
function App() {
return (
<Router>
<div>
<nav>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
</ul>
</nav>
<Routes>
<Route path="/about" element={<About />} />
<Route path="/" element={<Home />} />
</Routes>
</div>
</Router>
);
}
export default App;
Output:

Conclusion
React Router DOM, available via npm, simplifies client-side routing in React applications. It provides essential components like BrowserRouter, Router, Routes, and Route, facilitating URL-based navigation and rendering components based on URL paths. With its intuitive API, React Router DOM enables seamless navigation and state management, enhancing the user experience in single-page applications.
Similar Reads
react-router-dom - NPM react-router-dom is an important library for handling routing in React applications. It allows you to navigate between different components and manage the browser history. Here, we cover everything you need to know about react-router-dom, from installation using npm to implementing routes in a React
4 min read
What is react-router-dom ? React Router DOM is an npm package that enables you to implement dynamic routing in a web app. It allows you to display pages and allow users to navigate them. It is a fully-featured client and server-side routing library for React. React Router Dom is used to build single-page applications i.e. app
6 min read
React Router React Router is a library for handling routing and navigation in React JS Applications. It allows you to create dynamic routes, providing a seamless user experience by mapping various URLs to components. It enables navigation in a single-page application (SPA) without refreshing the entire page.This
6 min read
React-Router Hooks React-Router is a popular React library that is heavily used for client-side routing and offers single-page routing. It provides various Component APIs( like Route, Link, Switch, etc.) that you can use in your React application to render different components based on the URL pathnames on a single pa
11 min read
HashRouter in React Router React Router is a library in React JS (Frontend Framework). It is commonly used for handling the navigation and routing in a web application. In this post, we will learn about the "HashRouter" component provided by the React Router library. Table of Content What is HashRouter?Features of HashRouterD
2 min read
Link Component in React Router React Router is a powerful library in ReactJS that is used to create SPA (single-page applications) seamlessly. One of the components of the React Router is Link. In this article, we will be seeing the working of the Link component, various props, and usage of the Link component within React Router
5 min read