useNavigate to use in a React Component class where hooks cannot be used.
It’s recommended to avoid using this component in favor of useNavigate.
Type Declaration
Props
To
required
The path to navigate to. Can be a string or a Path object.
boolean
Whether to replace the current entry in the History stack instead of pushing a new one.
any
State to pass to the new Location to store in
history.state.RelativeRoutingType
How to interpret relative routing in the
to prop.- route (default) - Relative to the route hierarchy
- path - Relative to the URL path
Examples
Basic Redirect
With Replace
Conditional Navigation
With State
In Class Components
Index Route Redirect
Behavior
- Navigates immediately when rendered
- The navigation happens in a
useEffectto avoid render-time side effects - Cannot be used on the initial render in a
<StaticRouter>(server-side rendering) - Returns
null- does not render any DOM elements
Warnings
Alternative
In most cases, you should use theuseNavigate hook instead: