React Router is the foundational library for declarative client-side routing in React applications. It provides a robust, component-based approach to managing navigation and UI synchronization with URLs. The current stable version is 7.14.1, which builds upon the significant architectural changes introduced in v6, emphasizing a hooks-centric API and simpler routing patterns. The project maintains a relatively frequent release cadence, often issuing minor updates and patches, reflecting active development and community engagement. Key differentiators include its deep integration with the React component lifecycle, enabling dynamic and nested routing, and its focus on being a core part of the Remix ecosystem, offering a coherent experience for web development.
npm install react-routerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates basic routing with `BrowserRouter`, nested routes with `Outlet`, dynamic parameters with `useParams`, and navigation with `Link` in a typical React application.
Migrate `Switch` to `Routes`, `useHistory` to `useNavigate`, and `component`/`render` props to `element` prop for `Route` components. Review official migration guides for comprehensive changes.
Update your project to use ES modules (`import`/`export`) or use a bundler that transpiles ESM to CJS if necessary (though not recommended for newer Node environments). Ensure your `package.json` includes `"type": "module"` if using ESM directly in Node.
Ensure `BrowserRouter` wraps your entire application and is rendered only once, typically in `src/index.tsx` or `src/App.tsx`.
Review all `Link` `to` props and `useNavigate` calls. If you intend an absolute path, prefix it with `/`. For relative paths, understand the new base behavior, and use `..` for parent segments.
Ensure your project's `package.json` specifies `react` and `react-dom` versions compatible with `^18.0.0` or higher, matching the peer dependency requirements, and run `npm install`.
Change your import statement from `import { BrowserRouter } from 'react-router';` to `import { BrowserRouter } from 'react-router-dom';`Replace `useHistory()` with `useNavigate()` and adapt its usage. `useNavigate` returns a function to imperatively navigate, unlike `useHistory` which returned a history object.
Replace `component={MyComponent}` or `render={(props) => <MyComponent {...props} />}` with `element={<MyComponent />}`.Ensure all direct children of `<Routes>` are `<Route>` components or fragments containing them. If upgrading from v5, replace `<Switch>` with `<Routes>`.