DEV Community

Cover image for React Router DOM
Mahalakshmi C
Mahalakshmi C

Posted on

React Router DOM

What is React Router DOM?

React Router DOM is a library used in React applications to handle routing and navigation.

Routing means displaying different components or pages based on the URL.

For example, a blog application may have:

/              → Home
/about         → About
/blog          → Blog
/contact       → Contact
Enter fullscreen mode Exit fullscreen mode

React Router allows users to move between these pages without completely reloading the browser page.


Why Do We Use React Router?

React applications are often Single Page Applications (SPAs). This means the application can display different content without loading a completely new HTML page from the server for every navigation.

React Router helps us:

  • Create different routes
  • Navigate between pages
  • Display different components for different URLs
  • Create dynamic routes
  • Handle nested routes
  • Navigate programmatically

Installing React Router DOM

First, install the package:

npm install react-router-dom
Enter fullscreen mode Exit fullscreen mode

Then we can use it in our React application.


Important Components

Some commonly used React Router features are:

  • BrowserRouter
  • Routes
  • Route
  • Link
  • NavLink
  • useNavigate
  • useParams

Basic Example

Suppose we have a simple blog with three pages:

Home
Blog
About
Enter fullscreen mode Exit fullscreen mode

App.jsx

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

function Home() {
    return <h1>Home Page</h1>;
}

function Blog() {
    return <h1>Blog Page</h1>;
}

function About() {
    return <h1>About Page</h1>;
}

function App() {
    return (
        <BrowserRouter>

            <nav>
                <Link to="/">Home</Link>
                <Link to="/blog">Blog</Link>
                <Link to="/about">About</Link>
            </nav>

            <Routes>
                <Route path="/" element={<Home />} />
                <Route path="/blog" element={<Blog />} />
                <Route path="/about" element={<About />} />
            </Routes>

        </BrowserRouter>
    );
}

export default App;
Enter fullscreen mode Exit fullscreen mode

How Does This Code Work?

1. BrowserRouter

<BrowserRouter>
Enter fullscreen mode Exit fullscreen mode

BrowserRouter enables routing in the React application and uses the browser's URL to manage navigation.


2. Routes

<Routes>
Enter fullscreen mode Exit fullscreen mode

Routes contains all the routes defined in the application.


3. Route

<Route path="/blog" element={<Blog />} />
Enter fullscreen mode Exit fullscreen mode

A Route connects a URL path with a React component.

Here:

/blog → Blog component
Enter fullscreen mode Exit fullscreen mode

When the browser URL becomes /blog, React Router displays the Blog component.


4. Link

<Link to="/blog">Blog</Link>
Enter fullscreen mode Exit fullscreen mode

Link is used to navigate between routes.

When the user clicks Blog, React Router changes the URL and displays the Blog component without performing a full page reload.


Dynamic Routing

React Router can also create dynamic routes.

For example, a blog may have different posts:

/blog/1
/blog/2
/blog/3
Enter fullscreen mode Exit fullscreen mode

We can create a dynamic route using:

<Route path="/blog/:id" element={<BlogPost />} />
Enter fullscreen mode Exit fullscreen mode

Here, :id is a dynamic parameter.

We can access it using useParams().

import { useParams } from "react-router-dom";

function BlogPost() {

    const { id } = useParams();

    return <h1>Blog Post ID: {id}</h1>;
}
Enter fullscreen mode Exit fullscreen mode

If the URL is:

/blog/10
Enter fullscreen mode Exit fullscreen mode

the output will be:

Blog Post ID: 10
Enter fullscreen mode Exit fullscreen mode

useNavigate

useNavigate is used when we want to navigate to another route using JavaScript.

import { useNavigate } from "react-router-dom";

function Login() {

    const navigate = useNavigate();

    function handleLogin() {
        navigate("/home");
    }

    return (
        <button onClick={handleLogin}>
            Login
        </button>
    );
}
Enter fullscreen mode Exit fullscreen mode

After clicking the button, the user is navigated to /home.


React Router Flow

User clicks a Link
       ↓
URL changes
       ↓
React Router checks the URL
       ↓
Matching Route is found
       ↓
Corresponding Component is rendered
       ↓
UI is updated
Enter fullscreen mode Exit fullscreen mode

Important Points

  • BrowserRouter enables routing.
  • Routes contains the application's routes.
  • Route connects a URL path with a component.
  • Link is used for navigation.
  • useParams accesses dynamic URL parameters.
  • useNavigate allows navigation using JavaScript.
  • React Router allows navigation without a full page reload.

Conclusion

React Router DOM provides a simple way to implement navigation and routing in React applications. It is especially useful for applications with multiple views, such as blogs, dashboards, shopping websites, and portfolio applications.

Top comments (0)