Skip to main content

useNavigate

Returns a function that lets you navigate programmatically in the browser in response to user interactions or effects.
It’s often better to use redirect in action/loader functions than this hook.

Signature

Parameters

None.

Returns

NavigateFunction
A function for programmatic navigation with two overload signatures:String/Object Navigation:
string | To
required
The destination to navigate to. Can be a string path or an object with pathname, search, hash, and state properties.
NavigateOptions
Numeric Navigation:
number
required
Number of entries to go back (negative) or forward (positive) in the history stack.

Usage

Access the state on the next page:
Be cautious with navigate(number). There may not be a history entry to go back/forward to, or it might navigate to a different domain.

Replace current entry

This is useful for redirects where you don’t want the user to go back to the previous page.

Prevent scroll reset

Call in useEffect

Call navigate() in a React.useEffect(), not during component render.

Type Safety

Return Type Augmentation

The return type is void | Promise<void> because the implementation differs between Declarative mode and Data/Framework modes. To avoid TypeScript errors, augment the type based on your router: Declarative Mode (<BrowserRouter>):
Data/Framework Mode (<RouterProvider>):
  • <Link> - Declarative navigation component
  • <Navigate> - Declarative navigation component for redirects
  • redirect - Server-side redirect utility
  • useLocation - Access current location