useNavigate useNavigate Imperative Navigation

Last updated: 2026-11-02

useNavigate useNavigate Imperative Navigation

useNavigate returns a navigate function: go to the home page after signing in, return to the list after a submit, bounce anonymous users to the login page - all these code-driven jumps use it. navigate(to) pushes forward, navigate(-1) goes back, and you can pass replace and state along. It is the imperative counterpart of Link and the tool of choice whenever the code, not the user, decides where to go next.

GroupRouting & State Mgmt
KindHook
SinceReact Router 6.4

ℹ️ Info The editor transpiles JSX to JavaScript and renders the real UI in a sandbox; components mount automatically — no manual render call needed.

📝 Syntax

import { useNavigate } from 'react-router-dom'

function LoginButton() {
  const navigate = useNavigate()
  return (
    <button onClick={() => {
      // Navigate after a successful login; replace leaves no history entry
      navigate('/dashboard', { replace: true })
    }}>Sign in</button>
  )
}
// navigate(-1) is the browser back button
// navigate('/detail', { state: { from: 'home' } }) carries hidden state

⚙️ Parameters

toThe destination: a path string ('/home'), a number (-1 to go back) or an object of the form { pathname, search }.
optionsThe second argument: { replace: true } replaces the history entry, { state } carries data that does not show up in the address bar.

Returns:Returns the navigate(to, options?) function: calling it triggers the jump; it renders no UI and only changes the route.

▶ Example

The example runs out of the box: it auto-renders when expanded, and edits take effect instantly.

Output:

Clicking "Go to login" appends login to the history stack; "replace back home" overwrites the top entry without a trace; "Back" pops the stack and returns to the previous page - the push, replace and back behaviours of navigate (simulated demo).

💡 Key Points

💡 Tip After signing in prefer navigate('/home', { replace: true }): pressing back then does not return the user to the login page.
ℹ️ Info v6 removed useHistory in favour of useNavigate, which also handles numeric jumps such as navigate(-2).
⚠️ Warning Do not call navigate while rendering; put it in an event callback or an Effect, otherwise React reports "Cannot update during render".
More case studies coming soon.

📚 Related Tutorials

❓ FAQ

Q When do I use navigate and when do I use Link?
A Use Link when the user clicks a link; use navigate when the code decides to jump (a successful submit, a permission check, a countdown that just ended).
Q What does replace: true do?
A It replaces the current history entry with the new one, so going back skips that page - handy for login and payment intermediate pages, where a back navigation could trigger a duplicate action.
Q How do I pass data along with the navigation?
A Call navigate('/detail', { state: { from: 'home' } }) and read it on the target page with useLocation().state. It never shows up in the address bar and it survives a refresh inside history.state.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏