Navigation is one of the most fundamental concepts in React applications. Whether you're building a dashboard, an eCommerce store, or a portfolio website, knowing when to use Link, useNavigate, and Navigate (Redirect) will make your application more maintainable and provide a better user experience.
In this tutorial, we'll learn the differences between each navigation method, when to use them, and implement them step by step using React Router v7.
Prerequisites
Before starting, make sure you have:
- Basic knowledge of React
- Node.js installed
- React Router installed
If you haven't installed React Router yet:
npm install react-router
Project Structure
src/
βββ App.jsx
βββ main.jsx
βββ pages/
β βββ Home.jsx
β βββ About.jsx
β βββ Dashboard.jsx
β βββ Login.jsx
Step 1: Configure React Router
First, wrap your application with BrowserRouter.
import React from "react";
import ReactDOM from "react-dom/client";
import { BrowserRouter } from "react-router";
import App from "./App";
ReactDOM.createRoot(document.getElementById("root")).render(
<BrowserRouter>
<App />
</BrowserRouter>
);
Now define your routes.
import { Routes, Route } from "react-router";
import Home from "./pages/Home";
import About from "./pages/About";
import Dashboard from "./pages/Dashboard";
import Login from "./pages/Login";
function App() {
return (
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/dashboard" element={<Dashboard />} />
<Route path="/login" element={<Login />} />
</Routes>
);
}
export default App;
Step 2: Navigate Using Link
Link is used for navigation initiated by users.
Instead of refreshing the page like an HTML <a> tag, React Router updates the URL without reloading the application.
import { Link } from "react-router";
function Navbar() {
return (
<nav>
<Link to="/">Home</Link>
<Link to="/about">About</Link>
<Link to="/dashboard">Dashboard</Link>
</nav>
);
}
β Best Use Cases
- Navigation bars
- Sidebar menus
- Buttons behaving like links
- Breadcrumb navigation
Why use Link?
- No full page refresh
- Faster navigation
- Preserves application state
- Better user experience
Step 3: Navigate Programmatically with useNavigate
Sometimes users shouldn't click a link.
Examples include:
- Login success
- Form submission
- Checkout completed
- Logout
- Cancel button
For these situations, use useNavigate.
import { useNavigate } from "react-router";
function Login() {
const navigate = useNavigate();
function handleLogin() {
// authentication logic
navigate("/dashboard");
}
return (
<button onClick={handleLogin}>
Login
</button>
);
}
Replace Browser History
Prevent users from going back to the login page.
navigate("/dashboard", {
replace: true,
});
This replaces the current history entry.
Perfect for:
- Login
- Logout
- Payment success
- Password reset
Navigate Back
navigate(-1);
Navigate forward.
navigate(1);
Very useful for:
- Back buttons
- Multi-step forms
- Wizards
Step 4: Redirect Using <Navigate />
Sometimes navigation should happen automatically.
For example:
- User isn't logged in
- User lacks permission
- Page doesn't exist
Use the Navigate component.
import { Navigate } from "react-router";
function Dashboard() {
const isAuthenticated = false;
if (!isAuthenticated) {
return <Navigate to="/login" replace />;
}
return <h1>Dashboard</h1>;
}
Instead of rendering the dashboard, React redirects users immediately.
Authentication Example
A common protected route looks like this.
import { Navigate } from "react-router";
function ProtectedRoute({ children }) {
const isAuthenticated = false;
if (!isAuthenticated) {
return <Navigate to="/login" replace />;
}
return children;
}
Usage:
<Route
path="/dashboard"
element={
<ProtectedRoute>
<Dashboard />
</ProtectedRoute>
}
/>
Step 5: Which One Should You Use?
| Situation | Recommended |
|---|---|
| Navigation menu | Link |
| Sidebar | Link |
| User clicks page links | Link |
| Login success | useNavigate |
| Form submission | useNavigate |
| Checkout completed | useNavigate |
| Logout | useNavigate |
| Authentication guard | Navigate |
| Permission check | Navigate |
| Redirect unknown pages | Navigate |
Common Mistakes
β Using <a> tags
<a href="/dashboard">Dashboard</a>
This refreshes the entire application.
Instead use:
<Link to="/dashboard">
Dashboard
</Link>
β Using useNavigate inside JSX
Incorrect:
return navigate("/login");
Correct:
return <Navigate to="/login" replace />;
β Using Link after Login
Avoid:
<Link to="/dashboard">
Login
</Link>
Instead:
navigate("/dashboard");
Only navigate after authentication succeeds.
Best Practices
- β
Use
Linkfor user-initiated navigation. - β
Use
useNavigateafter events like login, form submission, or logout. - β
Use
Navigatefor conditional redirects. - β
Use
replace: truewhen users shouldn't return to the previous page. - β
Avoid using HTML
<a>tags for internal routing. - β Keep navigation logic predictable and easy to maintain.
Conclusion
Choosing the correct navigation method keeps your React application clean, scalable, and user-friendly.
A simple rule to remember is:
Linkβ User clicks to navigateuseNavigateβ Your JavaScript decides where to goNavigateβ React automatically redirects during rendering
Following these best practices will help you build applications that feel fast, modern, and intuitive.
πΊ Prefer Watching Instead?
If you'd rather follow along with a complete walkthrough, check out the full video tutorial:
π₯ React Navigation Explained: Link vs useNavigate vs Redirect (Best Practices!)
https://www.youtube.com/watch?v=BBWF5pQbCeU
π Learn to Build a Complete Modern React eCommerce App
If you want to go beyond navigation and build a real-world project from scratch, check out my complete React eCommerce series where you'll learn:
- ποΈ Product Listing
- π Product Details
- π Shopping Cart
- β€οΈ Wishlist
- π Authentication & Authorization
- π³ Stripe Payment Integration
- βοΈ Deployment
- π± Responsive Design
- β‘ React Router
- π₯ Firebase
- π Modern React Best Practices
π₯ Watch here:
https://www.youtube.com/watch?v=SdITjnl-zo8
If you found this article helpful, don't forget to β€οΈ like, save, and follow for more React tutorials. Happy coding! π
Top comments (1)
I particularly appreciated the distinction made between
LinkanduseNavigate, highlighting thatLinkis ideal for user-initiated navigation, such as in navigation bars or sidebar menus, whileuseNavigateis better suited for programmatically handling navigation, like after a login or form submission. The example withnavigate("/dashboard", { replace: true })to prevent users from going back to the login page after a successful login is a great illustration of how to manage browser history effectively. One thing that might be worth exploring further is how to handle more complex navigation flows, such as those involving multiple redirects or conditional navigation based on user roles or permissions.