React Router v7 is the first version that fully embraces modern React patterns, including server components, actions, and loaders, while still supporting the classic client-side routing you already know. This guide walks through what changed, how to upgrade or start fresh, and how to leverage the new data APIs without breaking existing apps.
What You’ll Need
- Node.js 20 or later
- An existing React app using React 18 or 19, or a new project scaffold
- Basic familiarity with React Router v6 (routes,
useNavigate,Link) - If you’re using Vite, the
@vitejs/plugin-reactplugin installed
Step-by-Step
- Understand the two modes. React Router v7 offers a “library mode” (just a drop-in upgrade from v6) and a “framework mode” (which adds loaders, actions, and server rendering). Decide which one fits your project. For existing client-side apps, start with library mode.
- Install v7. Run
npm install react-router-dom@latestornpm install react-router@latestdepending on your setup. In v7, the package is unified:react-routeris the main package, andreact-router-domre-exports it for compatibility. - Replace imports. In your source files, change
import { BrowserRouter, Routes, Route } from 'react-router-dom'toimport { BrowserRouter, Routes, Route } from 'react-router'. The API is identical, so no other changes are needed for basic routing. - Migrate data routers. If you were using
createBrowserRouterin v6, it’s still there. In v7, it now supports the newloaderandactionfunctions natively, so you can start moving data fetching into your route definitions. - Adopt loaders for data fetching. Instead of using
useEffectto fetch data, define aloaderon your route. The loader runs before the component renders, and the component receives the data viauseLoaderData. This is the core of the “modern React” bridge. - Use actions for mutations. For form submissions and mutations, define an
actionon the route. When a form is submitted withuseSubmitor a plainform, the action runs, and you can revalidate the data withuseRevalidator. - Leverage React Server Components (optional). If you’re using a framework like Vite with SSR, v7 supports server components. You can mark a component with
.server.jsxand it will only run on the server. This is the biggest leap toward modern React. - Update your
package.jsonscripts. If you were usingreact-scripts, you may need to switch to Vite or Next.js for full framework mode. For library mode, no script changes are needed. - Test your routes. Run your dev server and click through all routes. Check that
useParams,useSearchParams, anduseLocationstill work as before. - Remove deprecated APIs. v7 removes
useTransitionfrom React Router (use React’s ownuseTransitioninstead) anduseRouteMatch(useuseMatches). Search your codebase for these and replace them. - Consider the new
Routeobject syntax. While JSX routes still work, v7 encourages defining routes as plain objects withloader,action, andComponentproperties. This makes routes easier to test and reuse. - Enable framework mode (if needed). To use server rendering, install
@react-router/devand follow the Vite plugin setup. This turns your app into a full-stack React app with server-side data fetching.
Common Mistakes
- Keeping old imports. Forgetting to change
react-router-domtoreact-routercan cause duplicate package issues. Use the new package consistently. - Using
useEffectfor data when you have loaders. This defeats the purpose of the new data APIs and can cause double-fetching. Let loaders own the data fetching. - Mixing v6 and v7 packages. If you have both
react-router-dom@6andreact-router@7installed, you’ll get runtime errors. Remove the old package. - Not handling errors in loaders. If a loader throws, the route won’t render. Use
errorElementor aErrorBoundaryto catch these. - Assuming server components work in library mode. Server components require the framework mode with a server. In library mode, they’re ignored.
Tips
- Start with library mode if you have a large existing app. You can incrementally adopt loaders and actions without a full rewrite.
- Use the
createBrowserRouterwith object routes from the beginning, as it’s the recommended pattern for v7. - For new projects, use the official
create-react-routerscaffold to get the framework mode set up correctly. - Take advantage of
useFetcherfor non-navigation data mutations, like a like button or a comment form. - Keep your loaders focused on one resource. This makes caching and revalidation easier.
Troubleshooting
- Routes render blank after upgrade. Check that you’ve replaced all imports and that you don’t have two versions of React Router. Run
npm ls react-routerto see the tree. - Loader data is undefined. Ensure your component uses
useLoaderDataand that the loader is on the same route as the component. Also check that the loader returns a serializable object. - Form actions don’t fire. Make sure your
formhas amethodattribute (e.g.,method="post") and that the route has anactiondefined. Also check that you’re not using a plain HTML form without React Router’sFormcomponent. - Server components error in dev. If you’re using framework mode, ensure your Vite config includes the React Router plugin and that your server is running. Also check that your component files have the correct
.server.jsxextension. - TypeScript errors about
LoaderFunction. ImportLoaderFunctionfromreact-routerinstead ofreact-router-dom. The types are the same but the path changed. - Navigation is slow. If you’re using loaders, consider adding
deferto non-critical data to show the page immediately while the loader resolves.
Frequently Asked Questions
Do I need to rewrite my app to use v7?
No. Library mode is a drop-in replacement for v6. You can upgrade and then gradually adopt the new data APIs.
What’s the difference between react-router and react-router-dom in v7?
react-router is the core package. react-router-dom is still published as a re-export for backward compatibility, but you should import from react-router to avoid confusion.
Can I use React Router v7 with React 18?
Yes, v7 supports React 18 and 19. Some framework mode features like server components require React 19, but library mode works with both.
How do loaders replace useEffect for data fetching?
Instead of fetching in a component after render, a loader runs before the route renders. The component then reads the data via useLoaderData. This avoids loading states and race conditions.
Is React Router v7 the same as Remix?
No, but it’s heavily inspired by Remix. The data APIs (loaders, actions, fetchers) are similar. Remix is a full-stack framework built on top of React Router, while v7 brings those ideas into the core library.
What happens to useHistory in v7?
It was already deprecated in v6. Use useNavigate instead. The useHistory hook is not available in v7.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Custom Alphanumeric Passcode iPhone: How to Set
- HomePod Software Update: How to Install
- How to Shuffle Photos in Slideshow on iPhone
- Quick Look Not Working on Mac? Fixes That Work
- How to See Deleted WhatsApp Messages on iPhone
- QUIC Protocol Error in Windows 10: Fixes