π§ React Navigation with useNavigate & <Link> β Programmatic & Declarative Routing (2025)
π§² Introduction β Why Navigation Matters in React
In Single Page Applications (SPAs) built with React.js, navigation must be handled client-side without triggering full page reloads. React Router provides two main tools for this:
<Link>for declarative navigationuseNavigate()for programmatic navigation
These tools offer flexibility, accessibility, and a seamless user experience.
π― In this guide, youβll learn:
- When to use
<Link>vsuseNavigate - How to implement page transitions
- Handle redirects and button-based navigation
- Best practices for navigation in modern React apps
π§ 1. Declarative Navigation with <Link>
<Link> is a React Router component that renders an anchor tag but handles navigation without reloading the page.
β Basic Usage:
import { Link } from 'react-router-dom';
<Link to="/about">Go to About Page</Link>
β Advantages:
- No page reloads
- Built-in accessibility
- Works with nested routes
- Easy to use in menus, sidebars, or navbars
π― 2. Active Links with <NavLink>
<NavLink> is like <Link>, but it adds styling for active routes.
β Example:
import { NavLink } from 'react-router-dom';
<NavLink
to="/dashboard"
className={({ isActive }) => (isActive ? 'active-link' : '')}
>
Dashboard
</NavLink>
π Useful for tabs, sidebars, and breadcrumbs
βοΈ 3. Programmatic Navigation with useNavigate()
React Routerβs useNavigate() hook lets you navigate in response to logic, like after submitting a form or clicking a button.
β Example:
import { useNavigate } from 'react-router-dom';
function Home() {
const navigate = useNavigate();
const handleClick = () => {
navigate('/profile');
};
return <button onClick={handleClick}>Go to Profile</button>;
}
π 4. Redirect After Action
You can redirect after authentication, form submission, or API response.
Example β Redirect after login:
function Login() {
const navigate = useNavigate();
const handleLogin = () => {
// Simulate login
navigate('/dashboard');
};
return <button onClick={handleLogin}>Login</button>;
}
π navigate() replaces history.push() from older versions
π 5. Navigate with State or Replace
β Replace History Entry:
navigate('/home', { replace: true });
π Prevents adding the route to browser history (like redirects)
β Pass State Between Routes:
navigate('/success', { state: { from: 'checkout' } });
Access state with useLocation() in target component:
const location = useLocation();
console.log(location.state.from); // 'checkout'
π§© 6. Navigation in Nested Routes
Use relative or absolute paths for child routes.
<Link to="settings">Settings</Link> // relative to parent route
<Link to="/dashboard/settings">Settings</Link> // absolute path
β Prefer relative links inside nested components for cleaner paths
π Best Practices for Navigation
β
Use <Link> for static navigation (menus, buttons)
β
Use useNavigate() for logic-driven redirects
β
Avoid using raw <a href> unless pointing to external URLs
β
Style active links with <NavLink>
β
Use replace: true for redirects like login/logout
π Summary β Recap & Next Steps
React Router’s <Link> and useNavigate() give you both declarative and programmatic control over navigation. Mastering both ensures smooth transitions, flexible logic handling, and great UX across your React app.
π Key Takeaways:
- Use
<Link>for UI navigation without page reloads - Use
<NavLink>for highlighting active routes - Use
useNavigate()to redirect or trigger routing after events - Navigate with state and replace browser history when needed
- Keep navigation logic clean, modular, and accessible
βοΈ Real-World Relevance:
Used in all React apps with routingβfrom eCommerce sites with checkout flows to dashboards, admin panels, and blogs with category filters and detail pages.
β FAQ Section
β When should I use useNavigate() over <Link>?
β
Use useNavigate() when you need to redirect after an action, like a successful form submission or login.
β How do I pass data using navigate()?
β
Use the state option:
navigate('/thank-you', { state: { orderId: 123 } });
β Can I use both navigate() and <Link> in the same app?
β
Yes! They serve different purposes and can coexist seamlessly.
β What’s the difference between navigate('/path') and navigate('/path', { replace: true })?
β
replace: true replaces the current history entry instead of pushing a new one, useful for redirects.
β How do I highlight the current page in a menu?
β
Use <NavLink> and the isActive prop to conditionally apply CSS classes.
Share Now :
