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
A function for programmatic navigation with two overload signatures:String/Object Navigation:Numeric 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
number
required
Number of entries to go back (negative) or forward (positive) in the history stack.
Usage
Navigate to a path
Navigate with search params
Navigate with a To object
Navigate back/forward
Replace current entry
Prevent scroll reset
Call in useEffect
Type Safety
Return Type Augmentation
The return type isvoid | 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>):
<RouterProvider>):
Related
<Link>- Declarative navigation component<Navigate>- Declarative navigation component for redirectsredirect- Server-side redirect utilityuseLocation- Access current location