Skip to content
tezvyn:
Menu

useNavigate: Programmatic Navigation in React

Source: api.reactrouter.comMediumHow cards are made

The useNavigate hook gives you a function to change routes from your component's logic, like after a form submission. Use it for event-driven navigation, such as sending a user to their dashboard after login.

Why it exists

React Router's <Link> component handles user-clicked navigation. But systems often need to navigate based on code logic, like redirecting after a successful API call. useNavigate provides this imperative API for client-side routing from within your components.

The mental model

Think of useNavigate as a remote control for the browser's address bar that you can use from your JavaScript code. While a <Link> is a visible button for the user, the navigate function is something you call yourself when a certain condition is met, like a form being successfully submitted.

How it works

Call useNavigate() at the top level of your component to get a navigate function. You can call this function in a few ways. First, with a string path like navigate('/dashboard'). Second, with a number to move through the browser's session history, like navigate(-1) to go back. Third, with an options object to modify the navigation, such as navigate('/home', { replace: true }) to prevent the user from returning to the current page via the back button.

When to use it

Use useNavigate for navigation triggered by component logic. Common cases include: redirecting a user after a successful form submission (e.g., login or signup), creating a "Go Back" button that calls navigate(-1), or programmatically advancing a user through a multi-step wizard.

When not to use it

For standard navigation, always prefer the declarative <Link> component for accessibility. Inside modern React Router loader or action functions, it is better to return redirect('/path') than to use this hook. useNavigate is for client-side component logic, not data-layer redirects.

One canonical example

To redirect a user after a successful login and prevent them from going back to the login page, you would use the replace option. Inside your component:

const navigate = useNavigate();
const handleLogin = async () => {
const success = await api.login(credentials);
if (success) {
navigate('/dashboard', { replace: true });
}
};

This replaces the login page in the history stack with the dashboard page.

Interview question

Which situation most clearly calls for the imperative navigation provided by useNavigate?

  • a.Enabling users to move forward or backward through their browser's session history.
  • b.Defining static links in a header component for direct user interaction.
  • c.Returning a redirect response from a React Router loader function to enforce authentication.
  • d.Redirecting a user to a confirmation page only after a form passes server-side validation.Correct
Why?

useNavigate is designed for programmatic, event-driven navigation based on component logic, such as redirecting after a successful form submission. Returning a redirect from a loader (option C) is a data-layer redirect, which the card explicitly states is better handled by redirect('/path') directly.

Just read this? Test yourself on what you have been reading.

Read the original → api.reactrouter.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on react — each one lists the topics its interview covers.

See open roles