React Router
React Router is the core library for declarative routing in React applications, providing the foundational hooks and utilities for navigation and state management. The current stable version is 7.14.1, with frequent point releases reflecting active development and maintenance.
Common errors
-
Error: No routes matched URL "/"
cause The current URL path does not match any defined route in your router configuration.fixVerify your `createBrowserRouter` configuration to ensure that a route exists for the URL that is being accessed. Include `index: true` for default child routes or add a catch-all route. -
Error: You should not use `<Link>` outside a `Router`.
cause Routing components or hooks (e.g., `Link`, `useNavigate`) are being used in a component that is not rendered within a `RouterProvider` or `BrowserRouter` context.fixWrap your root application component (or the specific components that use routing elements) with `<RouterProvider router={router} />` or `<BrowserRouter>` to provide the necessary routing context. -
Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
cause React Router hooks (e.g., `useNavigate`, `useParams`, `useLocation`) are being called outside the scope of a React function component or custom hook.fixEnsure that all React Router hooks are called directly within the body of a React function component or a custom hook that itself follows React's rules of hooks. -
TypeError: Cannot read properties of undefined (reading 'Provider')
cause This error often occurs when `RouterProvider` is not correctly imported, or there's an incompatibility with React versions.fixVerify `import { RouterProvider } from 'react-router'` is correct and that your `react` and `react-dom` packages are both at version 18 or higher. -
ReferenceError: require is not defined
cause Attempting to use CommonJS `require()` syntax for imports in a modern JavaScript or TypeScript project configured for ESM (ES Modules).fixReplace `const RouterProvider = require('react-router').RouterProvider;` with `import { RouterProvider } from 'react-router';`.
Warnings
- breaking React Router v7 introduced significant changes, primarily with the data APIs (loaders, actions) and the recommended `createBrowserRouter` and `RouterProvider` setup.
- gotcha React Router v7 requires React and React DOM v18 or newer as peer dependencies.
- gotcha The `react-router` package is the core, but for browser-based applications, you typically install and use `react-router-dom` which exports browser-specific components like `BrowserRouter`.
- gotcha The package specifies a Node.js engine requirement.
Install
-
npm install react-router -
yarn add react-router -
pnpm add react-router
Imports
- RouterProvider
import { RouterProvider } from 'react-router'
Quickstart
import React from 'react';
import ReactDOM from 'react-dom/client';
import {
createBrowserRouter,
RouterProvider,
Outlet,
Link,
useNavigate
} from 'react-router';
function Layout() {
const navigate = useNavigate();
return (
<div>
<h1>React Router Example</h1>
<nav>
<Link to="/">Home</Link> | <Link to="/about">About</Link>
</nav>
<hr />
<Outlet /> {/* Renders the current route's element */}
<button onClick={() => navigate('/contact')}>Go to Contact</button>
</div>
);
}
function HomePage() {
return <h2>Welcome to the Home Page!</h2>;
}
function AboutPage() {
return <h2>Learn more about us!</h2>;
}
function ContactPage() {
return <h2>Get in touch!</h2>;
}
const router = createBrowserRouter([
{
path: '/',
element: <Layout />,
children: [
{
index: true,
element: <HomePage />,
},
{
path: 'about',
element: <AboutPage />,
},
{
path: 'contact',
element: <ContactPage />,
},
],
},
]);
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>
);