Registry / web-framework / react-router-dom-v5-compat

react-router-dom-v5-compat

JSON →
library6.30.3jsnpmunverified

The `react-router-dom-v5-compat` package provides a migration path for applications transitioning from React Router v4 or v5 to v6. It offers compatibility components and hooks that allow developers to run existing v5 routing logic within a v6 application environment. This enables incremental upgrades, avoiding a "big bang" rewrite. The package's current stable version is 6.30.3, aligning with the `react-router@6.x` series. While the core `react-router` project has progressed to v7, this compatibility layer remains focused on facilitating the v5-to-v6 migration. Its primary differentiator is enabling the co-existence of v5 and v6 routing paradigms, providing a temporary bridge rather than a permanent solution.

npm install react-router-dom-v5-compat
INSTALL
IMPORT
SIG · REACT-ROUTER-DOM-V
R
react-router-dom-v5-compat
web-frameworkjavascriptv6.30.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

CompatRouter
import { BrowserRouter as CompatRouter } from 'react-router-dom-v5-compat';
import { CompatRouter } from 'react-router-dom-v5-compat'; // Incorrect alias
This package re-exports the v5 `BrowserRouter` as `CompatRouter` to be used within a v6 application. You typically import it as an alias.
CompatSwitch
import { Switch as CompatSwitch } from 'react-router-dom-v5-compat';
import { CompatSwitch } from 'react-router-dom-v5-compat'; // Incorrect alias
Re-exports the v5 `Switch` component, which is crucial for v5 route matching logic. `Switch` was removed in v6.
useCompatHistory
import { useHistory as useCompatHistory } from 'react-router-dom-v5-compat';
import { useHistory } from 'react-router-dom-v5-compat'; // Ambiguous with v6's useNavigate
Re-exports the v5 `useHistory` hook. In v6, `useNavigate` is the successor, so using the aliased `useCompatHistory` makes it clear you're using the v5 equivalent.

Demonstrates how to integrate v5-style components and hooks using `react-router-dom-v5-compat` within a v6 `react-router-dom` application, allowing for a phased migration.

import React from 'react'; import ReactDOM from 'react-dom/client'; import { BrowserRouter as Router, Routes, Route, Link } from 'react-router-dom'; import { BrowserRouter as CompatRouter, Switch as CompatSwitch, useHistory as useCompatHistory } from 'react-router-dom-v5-compat'; // A v5-style component function OldHomePage() { const history = useCompatHistory(); const handleClick = () => { history.push('/old-about'); }; return ( <div> <h2>Old Home Page (v5 compat)</h2> <button onClick={handleClick}>Go to Old About</button> <Link to="/v6-dashboard">Go to v6 Dashboard</Link> </div> ); } // Another v5-style component function OldAboutPage() { return <h2>Old About Page (v5 compat)</h2>; } // A v6-style component function V6Dashboard() { return <h2>V6 Dashboard</h2>; } function App() { return ( <Router> <h1>Mixed React Router App</h1> <Routes> {/* v6 routes */} <Route path="/v6-dashboard" element={<V6Dashboard />} /> <Route path="/" element={ <CompatRouter> {/* v5 compatibility routes inside CompatRouter */} <CompatSwitch> <CompatRoute path="/old-about" component={OldAboutPage} /> <CompatRoute path="/" component={OldHomePage} /> </CompatSwitch> </CompatRouter> } /> </Routes> </Router> ); } const root = ReactDOM.createRoot(document.getElementById('root')); root.render(<App />);
Debug
Known issues
breakingThis package is a temporary migration tool for React Router v6. It is not intended for long-term use and applications should aim for full migration to native React Router v6 (or later v7) constructs to leverage all performance and API benefits.
fix
Prioritize refactoring v5-specific routing logic to use native `react-router-dom` v6 or v7 APIs (e.g., `useRoutes`, `useNavigate`, `Routes` component).
affects: >=6.0.0
gotchaMixing `react-router-dom` v5 and v6 contexts and hooks directly can lead to unexpected behavior or runtime errors. Ensure that v5 compatibility components/hooks (e.g., `useCompatHistory`) are only used within a `CompatRouter` context.
fix
Always wrap v5-dependent components with `CompatRouter` and use the aliased `Compat` prefixed hooks and components from `react-router-dom-v5-compat` when working with v5 logic.
affects: >=6.0.0
deprecatedThe underlying `react-router-dom` v5 itself is considered deprecated, with `react-router` having moved to v7. While `v5-compat` helps with the v6 transition, new development should target the latest stable version of `react-router-dom`.
fix
Plan and execute a complete migration to `react-router-dom` v6 or v7 as soon as feasible. Consult the official React Router migration guides for detailed steps.
affects: >=6.0.0
Errors
Common errors & fixes
Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
Attempting to use `useCompatHistory` or other `useCompat` hooks outside of a component rendered within a `CompatRouter` context.
fix
Ensure that any component using `useCompatHistory` or similar compatibility hooks is a child of `CompatRouter` (or `BrowserRouter as CompatRouter`).
Error: No routes matched location "/path"
This often indicates a mismatch between v5 and v6 routing configurations. For example, a v5 `CompatRoute` inside a v6 `Routes` without being nested in a `CompatSwitch` or `CompatRouter` that interprets v5 routes.
fix
Verify that v5 compatibility routes are correctly nested within `CompatRouter` and `CompatSwitch` components, and that the `path` props align with v5's exact/non-exact matching rules.
TypeError: Cannot read properties of undefined (reading 'push')
Likely attempting to access `history.push` from `useCompatHistory` before the history object is properly initialized by the `CompatRouter` or outside its scope.
fix
Confirm that the component calling `useCompatHistory` is rendered within the component tree established by `CompatRouter`.
Upgrade
Version history
6.30.3latest on npm
Audit
Dependencies
reactrequiredPeer dependency for React applications.
react-domrequiredPeer dependency for React DOM rendering.
react-router-domrequiredRequires `react-router-dom` v4 or v5 installed alongside v6 to provide the necessary legacy context and components for the compatibility layer.
Agent activity
4 hits · last 30 days
node
4
Resources
react-router-dom-v5-compat — npm install react-router-dom-v5-compat · libregistry