Skip to main content
A component-based version of 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 useEffect to 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

<Navigate> must not be used on the initial render in a <StaticRouter>. This is a no-op, but you should modify your code so the <Navigate> is only ever rendered in response to some user interaction or state change.

Alternative

In most cases, you should use the useNavigate hook instead:
Or use a loader to protect routes: