sodcalculator

10 Tech How-To

React Router v7: The Bridge to Modern React

Guide 10-59441 · Updated · Step by step

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-react plugin installed

Step-by-Step

  1. 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.
  2. Install v7. Run npm install react-router-dom@latest or npm install react-router@latest depending on your setup. In v7, the package is unified: react-router is the main package, and react-router-dom re-exports it for compatibility.
  3. Replace imports. In your source files, change import { BrowserRouter, Routes, Route } from 'react-router-dom' to import { BrowserRouter, Routes, Route } from 'react-router'. The API is identical, so no other changes are needed for basic routing.
  4. Migrate data routers. If you were using createBrowserRouter in v6, it’s still there. In v7, it now supports the new loader and action functions natively, so you can start moving data fetching into your route definitions.
  5. Adopt loaders for data fetching. Instead of using useEffect to fetch data, define a loader on your route. The loader runs before the component renders, and the component receives the data via useLoaderData. This is the core of the “modern React” bridge.
  6. Use actions for mutations. For form submissions and mutations, define an action on the route. When a form is submitted with useSubmit or a plain form, the action runs, and you can revalidate the data with useRevalidator.
  7. 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.jsx and it will only run on the server. This is the biggest leap toward modern React.
  8. Update your package.json scripts. If you were using react-scripts, you may need to switch to Vite or Next.js for full framework mode. For library mode, no script changes are needed.
  9. Test your routes. Run your dev server and click through all routes. Check that useParams, useSearchParams, and useLocation still work as before.
  10. Remove deprecated APIs. v7 removes useTransition from React Router (use React’s own useTransition instead) and useRouteMatch (use useMatches). Search your codebase for these and replace them.
  11. Consider the new Route object syntax. While JSX routes still work, v7 encourages defining routes as plain objects with loader, action, and Component properties. This makes routes easier to test and reuse.
  12. Enable framework mode (if needed). To use server rendering, install @react-router/dev and 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-dom to react-router can cause duplicate package issues. Use the new package consistently.
  • Using useEffect for 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@6 and react-router@7 installed, you’ll get runtime errors. Remove the old package.
  • Not handling errors in loaders. If a loader throws, the route won’t render. Use errorElement or a ErrorBoundary to 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 createBrowserRouter with object routes from the beginning, as it’s the recommended pattern for v7.
  • For new projects, use the official create-react-router scaffold to get the framework mode set up correctly.
  • Take advantage of useFetcher for 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-router to see the tree.
  • Loader data is undefined. Ensure your component uses useLoaderData and 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 form has a method attribute (e.g., method="post") and that the route has an action defined. Also check that you’re not using a plain HTML form without React Router’s Form component.
  • 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.jsx extension.
  • TypeScript errors about LoaderFunction. Import LoaderFunction from react-router instead of react-router-dom. The types are the same but the path changed.
  • Navigation is slow. If you’re using loaders, consider adding defer to 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

The whole Tech How-To bed →