Skip to main content

unstable_HistoryRouter

This is an unstable API. It’s highly discouraged to use your own history object as it may add two versions of the history library to your bundles unless you use the exact same version that React Router uses internally.
A low-level declarative <Router> that accepts a pre-instantiated history object. This component is primarily for advanced use cases where you need full control over the history instance.

Import

Type Declaration

Props

History
required
A pre-instantiated history object from the history library. This is the only required prop.Important: You must use the exact same version of the history library that React Router uses internally to avoid bundle size issues.
string
The base URL for all locations. If your app is served from a subdirectory, set this to the subdirectory path.
React.ReactNode
Your route configuration, typically <Routes> and <Route> elements.
boolean
Control whether router state updates are wrapped in React.startTransition.
  • undefined (default): All state updates are wrapped in startTransition
  • true: Link/Form navigations and all state updates use startTransition
  • false: Router doesn’t use startTransition at all
See React Transitions for more details.

Examples

Basic Usage

Always create the history instance outside of your component. Creating it inside will cause the history to be recreated on every render, breaking navigation.

With Custom History Configuration

Accessing History Outside React

One use case for HistoryRouter is when you need to access the history object outside of React components, such as in Redux middleware or analytics:

Testing with Custom History

When to Use

Use unstable_HistoryRouter only when:
  1. Redux integration: You need to access history in Redux middleware or sagas
  2. Analytics: You need to track navigation events outside React components
  3. Custom history: You need specific history library features not exposed by React Router
  4. Migration: You’re migrating from React Router v5 and need time to refactor
Using a custom history object has significant downsides:
  • Bundle size: May duplicate the history library in your bundle
  • Version conflicts: Must match React Router’s exact history version
  • Maintenance: More difficult to upgrade React Router
  • Stability: This API is marked as unstable and may change

Alternatives

For most use cases, use these alternatives instead:

BrowserRouter

Standard browser routing - use this for most apps

useNavigate

Programmatic navigation within React components

createBrowserRouter

Data Router API for loaders and actions

MemoryRouter

For testing - provides history without DOM

Migration from v5

If you’re migrating from React Router v5 and using a custom history:
Better: Refactor to use <BrowserRouter> or createBrowserRouter() and eliminate the custom history dependency.

See Also