➡ Dynamic Routes
Routes that capture variable parts of the URL, like /users/1, /users/42.
Define with a colon:
<Route path="/users/:id" element={<User />} />
Inside the User component, use useParams to grab the id:
import { useParams } from "react-router-dom";
function User() {
const { id } = useParams();
return <h1>User ID: {id}</h1>;
}
This lets you show different user profiles using the same component.
➡ useNavigate
A hook to change routes programmatically from JavaScript.
const navigate = useNavigate();
// After login:
navigate("/dashboard");
// Or go back:
navigate(-1);
Useful after form submissions, logins, or any action where you need to redirect without a link click.
➜Mental Model of Routing
React Router watches the URL. When it changes, it renders the matching component — no full page refresh. It feels instant.
Real App Structure
src/
├── components/ # reusable: Button, Card, Navbar
├── pages/ # full screens: Home, About, Profile
├── App.jsx
└── main.jsx
Pages are entire views that match a route; components are smaller pieces reused across pages. This keeps your project clean and easy to navigate.28 مه, 02:43
