🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEreact

react Documentation

LOADING ENGINE...

useNavigate

AI & DATA SCIENCE // usenavigate

useNavigate returns a function for programmatically navigating to a different route, used for redirects triggered by code rather than a user clicking a link.

Syntax

const navigate = useNavigate();
navigate('/path');

Deep Dive Course

Calling the function returned by useNavigate() with a path string navigates to that route, exactly as if the user had clicked a Link pointing there — this is the standard way to redirect programmatically in response to something other than a direct link click, like after a form successfully submits, after a login completes, or when a route guard determines the user shouldn't be on the current page. Passing { replace: true } as a second argument replaces the current entry in the browser's history stack instead of pushing a new one, useful for redirects that shouldn't leave the original page reachable via the back button, and passing a number, like navigate(-1), moves backward or forward through history exactly like the browser's own back/forward buttons.

1Understanding useNavigate

Calling the function returned by useNavigate() with a path string navigates to that route, exactly as if the user had clicked a Link pointing there — this is the standard way to redirect programmatically in response to something other than a direct link click, like after a form successfully submits, after a login completes, or when a route guard determines the user shouldn't be on the current page. Passing { replace: true } as a second argument replaces the current entry in the browser's history stack instead of pushing a new one, useful for redirects that shouldn't leave the original page reachable via the back button, and passing a number, like navigate(-1), moves backward or forward through history exactly like the browser's own back/forward buttons.

💡

Pass { replace: true } to navigate() for redirects that shouldn't remain reachable via the back button, like redirecting away from a login page immediately after a successful login — otherwise pressing back would return the user right back to the page they just left.

editor.html
import { useNavigate } from 'react-router-dom';

function LoginForm() {
  const navigate = useNavigate();
  const handleSubmit = (event) => {
    event.preventDefault();
    console.log('Login successful');
    navigate('/dashboard');
  };
  return <form onSubmit={handleSubmit}><button type="submit">Log In</button></form>;
}
localhost:3000

2Practical Example

Here is a real-world application of useNavigate showing how it is used in production React code.

editor.html
import { useNavigate } from 'react-router-dom';

function LoginPage() {
  const navigate = useNavigate();
  const handleLoginSuccess = () => navigate('/dashboard', { replace: true });
  return <button onClick={handleLoginSuccess}>Simulate Login</button>;
}
localhost:3000

3Best Practices

Follow these guidelines when working with useNavigate:

1. Use navigate() for redirects triggered by code, like after a successful form submission or login, rather than trying to simulate a link click

2. Pass { replace: true } when the current page shouldn't remain in history after the redirect, such as leaving a login or a completed checkout flow

3. Prefer a declarative Link component over calling navigate() for anything the user directly clicks, reserving navigate() specifically for programmatic redirects

⚠️

Tip: Pass { replace: true } to navigate() for redirects that shouldn't remain reachable via the back button, like redirecting away from a login page immediately after a successful login — otherwise pressing back would return the user right back to the page they just left.

editor.html
import { useNavigate } from 'react-router-dom';

function LoginForm() {
  const navigate = useNavigate();
  const handleSubmit = (event) => {
    event.preventDefault();
    console.log('Login successful');
    navigate('/dashboard');
  };
  return <form onSubmit={handleSubmit}><button type="submit">Log In</button></form>;
}
localhost:3000

Examples

Example 01Basic Usage
import { useNavigate } from 'react-router-dom';

function LoginForm() {
  const navigate = useNavigate();
  const handleSubmit = (event) => {
    event.preventDefault();
    console.log('Login successful');
    navigate('/dashboard');
  };
  return <form onSubmit={handleSubmit}><button type="submit">Log In</button></form>;
}
Example 02Advanced Example
import { useNavigate } from 'react-router-dom';

function LoginPage() {
  const navigate = useNavigate();
  const handleLoginSuccess = () => navigate('/dashboard', { replace: true });
  return <button onClick={handleLoginSuccess}>Simulate Login</button>;
}

Best Practices

  • Use navigate() for redirects triggered by code, like after a successful form submission or login, rather than trying to simulate a link click
  • Pass { replace: true } when the current page shouldn't remain in history after the redirect, such as leaving a login or a completed checkout flow
  • Prefer a declarative Link component over calling navigate() for anything the user directly clicks, reserving navigate() specifically for programmatic redirects

Interview Question

Why would you pass { replace: true } to navigate() after a successful login redirect, rather than letting it push a normal new history entry?

Hint: Think about what pressing the browser's back button should, and shouldn't, do immediately after that particular redirect.

By default, navigate() pushes a brand-new entry onto the browser's history stack, meaning the page you navigated away from remains reachable by pressing the back button, which is exactly the normal, expected behavior for most navigation, like clicking through a series of content pages. After a successful login redirect specifically, though, the login page itself typically shouldn't remain a meaningful destination to go back to, since the user is now authenticated and the login form no longer represents a valid or sensible state to return to. Passing { replace: true } swaps out the current history entry, the login page, for the new destination, the dashboard, rather than adding an additional entry on top of it, so pressing back afterward skips over the login page entirely and goes to whatever page came before it, avoiding the confusing experience of the back button landing the user right back on a login screen they've already passed.

Exercises

MediumPractice using useNavigate in a real scenario.
View Solution
import { useNavigate } from 'react-router-dom';

function LoginForm() {
  const navigate = useNavigate();
  const handleSubmit = (event) => {
    event.preventDefault();
    console.log('Login successful');
    navigate('/dashboard');
  };
  return <form onSubmit={handleSubmit}><button type="submit">Log In</button></form>;
}

Frequently Asked Questions

Why would you pass { replace: true } to navigate() after a successful login redirect, rather than letting it push a normal new history entry?

By default, navigate() pushes a brand-new entry onto the browser's history stack, meaning the page you navigated away from remains reachable by pressing the back button, which is exactly the normal, expected behavior for most navigation, like clicking through a series of content pages. After a successful login redirect specifically, though, the login page itself typically shouldn't remain a meaningful destination to go back to, since the user is now authenticated and the login form no longer represents a valid or sensible state to return to. Passing { replace: true } swaps out the current history entry, the login page, for the new destination, the dashboard, rather than adding an additional entry on top of it, so pressing back afterward skips over the login page entirely and goes to whatever page came before it, avoiding the confusing experience of the back button landing the user right back on a login screen they've already passed.

Related Functions

react-router-domuselocationlink