Open In App

Routes Component in React Router

Routes are an integral part of React Router, facilitating the navigation and rendering of components based on URL patterns.

In this article, we’ll delve into the concept of routes, understanding their role in React Router, and how to define and manage routes effectively.



What are Routes ?

In the context of React Router, routes define the mapping between URL patterns and the corresponding React components to render. They serve as the backbone of client-side navigation in single-page applications (SPAs), allowing users to navigate between different views without full-page reloads.



Why Routes Are Essential ?

Routes play a crucial role in building dynamic and interactive web applications. Here’s why they are essential:

Defining Routes:

In React Router, routes are typically defined using the Route component. Here’s the basic structure of a route definition:

<Route path="/example" component={ExampleComponent} />

Managing Routes

React Router provides various components and utilities for managing routes effectively:

Steps to implement Routes Component:

Step 1: Create react application with the help of following command.

npx create-react-app react-router
cd react-router

Step 2: Install the required dependencies.

npm I react-router-dom

Folder Structure:

Folder Structure

The updated dependencies in package.json file will look like:

"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.22.1",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}

Example of Routes Component

Code: Let’s consider an example to demonstrate the usage of routes in React Router:




//App.js
 
import React from 'react';
import {
    BrowserRouter as Router,
    Route, Routes
} from 'react-router-dom';
import Home from './components/Home';
import About from './components/About';
import Contact from './components/Contact';
import NotFound from './components/NotFound';
 
function App() {
    return (
        <Router>
            <div>
                <Routes>
                    <Route path="/" element={<Home />} />
                    <Route path="/about" element={<About />} />
                    <Route path="/contact" element={<Contact />} />
                    <Route path="*" element={<NotFound />} />
                </Routes>
            </div>
        </Router>
    );
}
 
export default App;




//Home.js
 
import React from 'react'
 
const Home = () => {
    return (
        <h1>Home</h1>
    )
}
 
export default Home




//Contact.js
 
import React from 'react'
 
const Contact = () => {
    return (
        <h1>Contact</h1>
    )
}
 
export default Contact




//About.js
 
import React from 'react'
 
const About = () => {
    return (
        <h1>About</h1>
    )
}
 
export default About




//NotFound.js
 
import React from 'react'
 
const NotFound = () => {
    return (
        <h1>NotFound</h1>
    )
}
 
export default NotFound

Output:

Explanation of Output:

Conclusion:

Routes are the backbone of client-side navigation in React Router, providing a powerful mechanism for defining URL-based navigation and rendering. Understanding how to define and manage routes effectively is essential for building dynamic and interactive React applications with seamless navigation capabilities.


Article Tags :