Without Router
URL change usually triggers a full page reload and server request.
React Router enables client-side routing in React applications. That means your URL can change and React can render a different component without a full page reload.
Without Router
URL change usually triggers a full page reload and server request.
With React Router
URL change updates only the relevant React components.
Install the package:
npm install react-router-domWrap your app:
import { BrowserRouter } from "react-router-dom";
function App() { return ( <BrowserRouter> <Main /> </BrowserRouter> );}import { Routes, Route } from "react-router-dom";
function Main() { return ( <Routes> <Route path="/" element={<Home />} /> <Route path="/about" element={<About />} /> {/* Nested Route / Child Routes */} <Route path="/user" element={<UserLayout />}> <Route path="profile" element={<Profile />} /> <Route path="settings" element={<Settings />} /> </Route> </Routes> );}import { useRoutes } from "react-router-dom";
function Main() { const routes = useRoutes([ { path: "/", element: <Home /> }, { path: "/about", element: <About /> }, // Nested Route / Child Routes { path: "/user", element: <UserLayout />, children: [ { path: "profile", element: <Profile /> }, { path: "settings", element: <Settings /> }, ], }, ]);
return routes;}| Feature | JSX Routes | Object Routes |
|---|---|---|
| Readability | Easy | Moderate |
| Scalability | Medium | High |
| Dynamic config | Hard | Easy |
| Used in large apps | Less | More |
Link handles navigation without reloading the page.
import { Link } from "react-router-dom";
<Link to="/about">Go to About</Link>;NavLink is similar to Link, but it also knows whether the route is active.
import { NavLink } from "react-router-dom";
<NavLink to="/about">About</NavLink>;| Feature | Link | NavLink |
|---|---|---|
| Navigation | Yes | Yes |
| Active styling | No | Yes |
| Use case | Basic navigation | Navbar and menus |
<NavLink to="/about" className={({ isActive }) => (isActive ? "active" : "")]}> About</NavLink>.active { color: red; font-weight: bold;}Dynamic routes help render pages like user profiles and products.
/users/1/users/2/users/3Define dynamic route:
<Route path="/user/:id" element={<User />} />Meaning:
:id = dynamic variableAccess dynamic parameter:
import { useParams } from "react-router-dom";
function User() { const { id } = useParams();
return <h1>User ID: {id}</h1>;}Route:
<Route path="/user/:id" element={<User />} />Component:
import { useParams } from "react-router-dom";import { useEffect, useState } from "react";
function User() { const { id } = useParams(); const [user, setUser] = useState(null);
useEffect(() => { fetch(`https://api.example.com/users/${id}`) .then((res) => res.json()) .then((data) => setUser(data)); }, [id]);
if (!user) return <p>Loading...</p>;
return ( <div> <h1>{user.name}</h1> <p>{user.email}</p> </div> );}<Route path="/user" element={<UserLayout />}> <Route path="profile" element={<Profile />} /> <Route path="settings" element={<Settings />} /></Route>Use useNavigate when navigation should happen from code (for example after form submit).
import { useNavigate } from "react-router-dom";
const navigate = useNavigate();
navigate("/about");React Router uses a path matching algorithm.
/user/:id matches:/user/1/user/abcuseEffect(() => { fetchData();}, []); // Wrong when id is used insideCorrect:
useEffect(() => { fetchData(id);}, [id]);<a href="/about">This reloads page</a><Route path="/user/id" /> // Wrong