How to Navigate Programmatically with useNavigate

Use the useNavigate hook to change the URL from code. Redirect after a form submission, go back, replace history entries, and pass state to the next page.

5 min read

The useNavigate hook returns a function that changes the URL from code. You use it when the user is not clicking a link but the app still needs to move, like after a form submits or a session expires.

It is the programmatic counterpart to Link, for moments when no element was clicked.

Get the navigate function

Call useNavigate inside a component to receive the navigate function, then call that function with a path to move the user there.

App.jsxApp.jsx
import { useNavigate } from "react-router";
 
export default function Login() {
  const navigate = useNavigate();
  function handleSubmit(event) {
    event.preventDefault();
    navigate("/dashboard");
  }
  return <form onSubmit={handleSubmit}><button type="submit">Log in</button></form>;
}

After the handler runs, the URL becomes /dashboard and React Router renders the matching route. The preventDefault call stops the browser's own form submission, then navigate takes over.

You can call navigate from event handlers, Effects, and async callbacks. It is a side effect, so it never belongs in the render path itself.

Go back and replace history

The function also accepts a number, where -1 steps back one entry. Pass replace to swap the current entry instead of adding a new one.

App.jsxApp.jsx
navigate(-1);
navigate("/checkout", { replace: true });

Going back is common for closing a modal or finishing a wizard step. Replace is useful after login, so the back button does not return the user to the login page.

Only call navigate with a number when you know the history stack has that entry.

Pass state to the next page

The second argument can carry state, which the target page reads with useLocation.

App.jsxApp.jsx
navigate("/order/42", { state: { fromCheckout: true } });

The state travels with the history entry rather than the URL. It survives only that navigation, so the next page reads it once.

App.jsxApp.jsx
import { useLocation } from "react-router";
 
export default function Order() {
  const location = useLocation();
  const fromCheckout = location.state?.fromCheckout;
  return <h1>Order placed</h1>;
}

Use state for small hints like where the user came from. Do not put data that must survive a refresh here, because it disappears on reload.

Reading location.state with optional chaining keeps the page from crashing when a user opens it directly.

useNavigate is for navigation the user did not click. For ordinary links, use Link or NavLink, which render real anchors with keyboard support, right-click menus, and open-in-new-tab behavior.

A button that only changes pages should often be a Link styled as a button, as shown in the React Router tutorial. When the target path contains a changing value, build it with a parameter, covered in dynamic routes and URL parameters.

The same call can also be triggered by a timer, a quiz step, or a session timeout.

Common mistakes

  • Using useNavigate for a normal link instead of Link.
  • Calling navigate during render rather than in an event or Effect.
  • Assuming state survives a page refresh.
Rune AI

Rune AI

Key Insights

  • Call useNavigate to get the navigate function.
  • Navigate after actions like login, logout, or form submission.
  • Use navigate(-1) to go back and replace to rewrite history.
  • Prefer Link for any navigation the user clicks.
RunePowered by Rune AI

Frequently Asked Questions

When should I use useNavigate instead of Link?

Use Link for clicks, because it renders a real anchor with keyboard and right-click support. Use useNavigate when navigation happens without a click, like after a form submits.

How do I go back a page?

Call navigate(-1) to step back one entry. Pass 1 to go forward, but only when you know the history stack has that entry.

Does state survive a page refresh?

No. State passed to navigate lives in the history entry, not the URL. It disappears on reload, so use it for short-lived hints.

Conclusion

useNavigate returns a function that changes the URL from code. Call it after form submissions, timeouts, or logout, pass replace to rewrite history, and use state for short-lived hints. Keep Link for ordinary clicks.