React Router DOM
React Router DOM (version 7.14.1) provides declarative routing for React web applications. It currently serves as a compatibility layer for projects migrating from React Router v6 to v7, re-exporting all components and hooks from the `react-router` package. While actively maintained with frequent minor and patch releases, its primary purpose is to facilitate migration, and users are encouraged to eventually remove it and import directly from `react-router` in v7 projects.
Common errors
-
Module not found: Error: Can't resolve 'react-router-dom'
cause The `react-router-dom` package is missing from your project's dependencies or was prematurely removed after a v7 migration.fixIf migrating to v7, ensure all imports have been changed to `react-router`. Otherwise, install `react-router-dom` via `npm install react-router-dom` or `yarn add react-router-dom`. -
Error: useRoutes() may be used only in the context of a <Router> component.
cause React Router hooks like `useRoutes` (or components like `Routes` and `Route`) are used outside of a `BrowserRouter` (or other router) component.fixWrap your application's root component or the section using routing components with `<BrowserRouter>`. For example: `<BrowserRouter><App/></BrowserRouter>`. -
TypeError: Cannot read properties of undefined (reading 'Provider')
cause Your project is likely using an older version of React (e.g., React 17) while `react-router-dom` v7 expects React 18+ with its new root API.fixUpgrade `react` and `react-dom` in your `package.json` to version `18.0.0` or higher: `npm install react@^18 react-dom@^18`.
Warnings
- breaking For React Router v7 and newer, `react-router-dom` is no longer the primary package for web routing. All components and hooks are now directly available from `react-router`.
- deprecated The `react-router-dom` package is intended as an upgrade path from v6 to v7 of React Router and is recommended to be removed once your application has successfully migrated.
- gotcha `react-router-dom` requires React 18 or newer as a peer dependency, meaning your project must use React version 18 or higher.
- gotcha This package requires Node.js version 20.0.0 or higher. Older Node.js versions may lead to build errors or runtime issues.
Install
-
npm install react-router-dom -
yarn add react-router-dom -
pnpm add react-router-dom
Imports
- BrowserRouter
const BrowserRouter = require('react-router-dom')import { BrowserRouter } from 'react-router-dom'
Quickstart
import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';
function App() {
return (
<div>
<nav>
<Link to="/">Home</Link> | <Link to="/about">About</Link>
</nav>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="*" element={<NoMatch />} />
</Routes>
</div>
);
}
function Home() {
return <h2>Home</h2>;
}
function About() {
return <h2>About</h2>;
}
function NoMatch() {
return <h3>Nothing to see here!</h3>;
}
const root = ReactDOM.createRoot(document.getElementById('root')!);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);