React Router v5 Compatibility Layer

6.30.3 · maintenance · verified Tue Apr 21

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.

Common errors

Warnings

Install

Imports

Quickstart

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 />);

view raw JSON →