DEV Community

Arhan Khan
Arhan Khan

Posted on

React Router for Beginners: Navigation in React Without the Confusion

React Router is the standard way to add multiple pages to a React app. Without it, your app is one long page. With it, /home, /about, and /profile become real URLs the user can bookmark, share, and navigate with the back button.

Setup is simple:

npm install react-router-dom
Enter fullscreen mode Exit fullscreen mode
import { BrowserRouter, Routes, Route, Link } from "react-router-dom";

function App() {
  return (
    <BrowserRouter>
      <nav>
        <Link to="/">Home</Link>
        <Link to="/about">About</Link>
      </nav>
      <Routes>
        <Route path="/" element={<Home />} />
        <Route path="/about" element={<About />} />
        <Route path="/users/:id" element={<UserProfile />} />
      </Routes>
    </BrowserRouter>
  );
}
Enter fullscreen mode Exit fullscreen mode

Dynamic routes use :id in the path. Inside the component, read it with a hook:

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

function UserProfile() {
  const { id } = useParams();
  return <h1>Profile of user {id}</h1>;
}
Enter fullscreen mode Exit fullscreen mode

Navigation in code uses useNavigate — for example, sending the user to a dashboard after login:

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

const navigate = useNavigate();
navigate("/dashboard");
Enter fullscreen mode Exit fullscreen mode

Nested routes let you build layouts once and reuse them. A dashboard layout with a sidebar stays in place while only the inner content changes between pages — that is how most real apps are structured.

Who is it for? Anyone building a React app with more than one screen. It is basically required for dashboards, blogs, shops, and anything with user accounts.

The limits: it only handles client-side routing, so SEO for public pages needs extra work like server-side rendering. And if your app is a single screen, adding a router is unnecessary complexity.

Start today: add two routes and one dynamic route to a practice project, plus a link between them. That is the whole core of routing — everything else is built on top of it.

Top comments (0)