Navigate after an async side effect
A successful form submission or mutation should move the user to another route.
TanStack · Hook
useNavigate returns an imperative navigation function for a TanStack Router application. Use it to change the pathname, search parameters, hash, or location state, especially when navigation is triggered by component logic.
In a React component, call useNavigate with an optional source location to obtain an imperative navigation function. Invoke that function with NavigateOptions to change the pathname, search parameters, hash, or location state; it returns a promise that resolves after navigation completes.
useNavigate(opts?: { from?: string }): (options: NavigateOptions) => Promise<void>| Name | Type | Description |
|---|---|---|
opts | object | Optional object configuring the hook; its from property is a string identifying the location to navigate from. |
options | NavigateOptions | A navigation configuration object, including destination and supported location updates. |
A navigate function; calling it returns a Promise that resolves when navigation is complete.
from option establishes the source location used to resolve navigation.import { useNavigate } from '@tanstack/react-router'
function PostsPage() {
const navigate = useNavigate({ from: '/posts' })
return (
<button onClick={() => navigate({ search: { page: 2 } })}>
Next page
</button>
)
}This component creates a navigate function scoped from /posts and uses it to update the page search parameter.
A successful form submission or mutation should move the user to another route.
A pagination, filtering, or sorting control needs to update URL state from an event handler.
A component must change the hash or location state without using a declarative link.
useNavigate is a hook that returns a navigate function for changing the current location. The navigation can modify the pathname, search parameters, hash, and location state.
Follow-up: How can navigation be scoped to a route? Pass the route location through the hook's optional from option, such as useNavigate({ from: '/posts' }).
The hook accepts an optional options object. Its from string identifies the location to navigate from, which is useful when the target should be resolved relative to a specific location rather than the current one.
Follow-up: What does the returned function accept? One options object of type NavigateOptions, containing the destination and other navigation settings.
The returned navigate function accepts one NavigateOptions object and performs the requested navigation. Its result is a promise that resolves when navigation finishes.
Follow-up: How would you update only a search parameter? Invoke navigate with a search option, for example navigate({ search: { page: 2 } }).
LinkNavigateOptionsuseSearchuseParamsRouteApi