从 Remix 升级
本页内容

从 Remix 升级

React Router v7 需要以下最低版本

  • node@20
  • react@18
  • react-dom@18

React Router v7 是 Remix v2 之后的下一个主要版本(有关更多信息,请参阅我们的《React 19 渐进之路》博客文章)。

如果您已启用所有 Remix v2 的 future flags,从 Remix v2 升级到 React Router v7 主要涉及更新依赖。

步骤 2-8 的大部分可以通过社区成员 James Restall 创建的 codemod 自动更新。

1. 采用 future flags

👉 采用 future flags

在您的 Remix v2 应用中采用所有现有的 future flags

2. 更新依赖

大多数曾通过运行时特定包(如 @remix-run/node@remix-run/cloudflare 等)重新导出的“共享”API,在 v7 中都已合并到 react-router 中。因此,您不再需要从 @react-router/node@react-router/cloudflare 导入,而是直接从 react-router 导入这些 API。

-import { redirect } from "@remix-run/node";
+import { redirect } from "react-router";

在 v7 中,您应仅从运行时特定包导入那些特定于该运行时的 API,例如用于 Node 的 createFileSessionStorage 和用于 Cloudflare 的 createWorkersKVSessionStorage

👉 运行 codemod(自动化)

您可以使用以下 codemod 自动更新您的包和导入。此 codemod 会更新您的所有包和导入。运行 codemod 前务必提交所有待处理的更改,以防需要回滚。

npx codemod remix/2/react-router/upgrade

👉 安装新的依赖

codemod 更新您的依赖后,您需要安装依赖以移除 Remix 包并添加新的 React Router 包。

npm install

👉 更新依赖(手动)

如果您不想使用 codemod,可以手动更新您的依赖。

展开以查看按字母顺序排列的包名称更改表
Remix v2 包 React Router v7 包
@remix-run/architect ➡️ @react-router/architect
@remix-run/cloudflare ➡️ @react-router/cloudflare
@remix-run/dev ➡️ @react-router/dev
@remix-run/express ➡️ @react-router/express
@remix-run/fs-routes ➡️ @react-router/fs-routes
@remix-run/node ➡️ @react-router/node
@remix-run/react ➡️ react-router
@remix-run/route-config ➡️ @react-router/dev
@remix-run/routes-option-adapter ➡️ @react-router/remix-routes-option-adapter
@remix-run/serve ➡️ @react-router/serve
@remix-run/server-runtime ➡️ react-router
@remix-run/testing ➡️ react-router

3. 修改 scripts in package.json

如果你使用了 codemod,可以跳过此步骤,因为它已自动完成。

👉 更新你的 package.json 中的 scripts

脚本 Remix v2 React Router v7
dev remix vite:dev ➡️ react-router dev
build remix vite:build ➡️ react-router build
start remix-serve build/server/index.js ➡️ react-router-serve build/server/index.js
类型检查 tsc ➡️ react-router typegen && tsc

4. 添加 routes.ts 文件

如果你使用了 codemod 并且使用了 Remix v2 的 v3_routeConfig 标志,可以跳过此步骤,因为它已自动完成。

在 React Router v7 中,你使用 app/routes.ts 文件定义路由。查看路由文档了解更多信息。

👉 更新依赖项(如果使用 Remix v2 的 v3_routeConfig 标志)

// app/routes.ts
-import { type RouteConfig } from "@remix-run/route-config";
-import { flatRoutes } from "@remix-run/fs-routes";
-import { remixRoutesOptionAdapter } from "@remix-run/routes-option-adapter";
+import { type RouteConfig } from "@react-router/dev/routes";
+import { flatRoutes } from "@react-router/fs-routes";
+import { remixRoutesOptionAdapter } from "@react-router/remix-routes-option-adapter";

export default [
  // however your routes are defined
] satisfies RouteConfig;

👉 添加一个 routes.ts 文件(如果没有使用 Remix v2 的 v3_routeConfig 标志)

touch app/routes.ts

为了向后兼容以及那些喜欢基于文件的约定的人,你可以通过新的 @react-router/fs-routes 包选择启用与你在 Remix v2 中使用的相同的“扁平路由”约定

import { type RouteConfig } from "@react-router/dev/routes";
import { flatRoutes } from "@react-router/fs-routes";

export default flatRoutes() satisfies RouteConfig;

或者,如果你之前使用 routes 选项来定义基于配置的路由

import { type RouteConfig } from "@react-router/dev/routes";
import { remixRoutesOptionAdapter } from "@react-router/remix-routes-option-adapter";

export default remixRoutesOptionAdapter((defineRoutes) => {
  return defineRoutes((route) => {
    route("/", "home/route.tsx", { index: true });
    route("about", "about/route.tsx");
    route("", "concerts/layout.tsx", () => {
      route("trending", "concerts/trending.tsx");
      route(":city", "concerts/city.tsx");
    });
  });
}) satisfies RouteConfig;

如果你之前在 vite.config.ts 中使用了 routes 选项,请务必将其删除。

export default defineConfig({
  plugins: [
    remix({
      ssr: true,
-     ignoredRouteFiles: ['**/*'],
-     routes(defineRoutes) {
-       return defineRoutes((route) => {
-         route("/somewhere/cool/*", "catchall.tsx");
-       });
-     },
    })
    tsconfigPaths(),
  ],
});

5. 添加 React Router 配置

👉 将 react-router.config.ts 添加到你的项目

之前传递给 vite.config.tsremix 插件的配置现在从 react-router.config.ts 导出。

注意:此时,你应该移除你在步骤 1 中添加的 v3 未来标志。

touch react-router.config.ts
// vite.config.ts
export default defineConfig({
  plugins: [
-   remix({
-     ssr: true,
-     future: {/* all the v3 flags */}
-   }),
+   reactRouter(),
    tsconfigPaths(),
  ],
});

// react-router.config.ts
+import type { Config } from "@react-router/dev/config";
+export default {
+  ssr: true,
+} satisfies Config;

6. 添加 React Router 插件到 vite.config

如果你使用了 codemod,可以跳过此步骤,因为它已自动完成。

👉 将 reactRouter 插件添加到 vite.config

修改 vite.config.ts 以导入并使用来自 @react-router/dev/vite 的新 reactRouter 插件

-import { vitePlugin as remix } from "@remix-run/dev";
+import { reactRouter } from "@react-router/dev/vite";
import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";

export default defineConfig({
  plugins: [
-   remix(),
+   reactRouter(),
    tsconfigPaths(),
  ],
});

7. 启用类型安全

如果你没有使用 TypeScript,可以跳过此步骤。

React Router 会自动为你的路由模块在应用根目录下生成类型文件到 .react-router/ 目录中。此目录完全由 React Router 管理,应添加到 .gitignore 中。了解更多关于新类型安全特性的信息。

👉 将 .react-router/ 添加到 .gitignore

.react-router/

👉 更新 tsconfig.json

更新你的 tsconfig.json 中的 types 字段以包含

  • include 字段中添加 .react-router/types/**/* 路径
  • types 字段中添加相应的 @react-router/*
  • rootDirs 用于简化相对导入
{
  "include": [
    /* ... */
+   ".react-router/types/**/*"
  ],
  "compilerOptions": {
-   "types": ["@remix-run/node", "vite/client"],
+   "types": ["@react-router/node", "vite/client"],
    /* ... */
+   "rootDirs": [".", "./.react-router/types"]
  }
}

8. 重命名入口文件中的组件

如果你使用了 codemod,可以跳过此步骤,因为它已自动完成。

如果你的应用中有 entry.server.tsx 和/或 entry.client.tsx 文件,你需要更新这些文件中的主要组件

-import { RemixServer } from "@remix-run/react";
+import { ServerRouter } from "react-router";

-<RemixServer context={remixContext} url={request.url} />,
+<ServerRouter context={remixContext} url={request.url} />,
-import { RemixBrowser } from "@remix-run/react";
+import { HydratedRouter } from "react-router/dom";

hydrateRoot(
  document,
  <StrictMode>
-   <RemixBrowser />
+   <HydratedRouter />
  </StrictMode>,
);

9. 更新 AppLoadContext 的类型

如果你之前使用 remix-serve,可以跳过此步骤。这仅适用于你在 Remix v2 中使用了自定义服务器的情况。

由于 React Router 既可以作为 React 框架使用,可以作为独立的路由库使用,LoaderFunctionArgsActionFunctionArgscontext 参数现在是可选的,并且默认类型为 any。你可以为你的加载上下文注册类型,以便为你的 loader 和 action 提供类型安全。

👉 为你的加载上下文注册类型

在迁移到新的 Route.LoaderArgsRoute.ActionArgs 类型之前,你可以暂时使用你的加载上下文类型来增强 LoaderFunctionArgsActionFunctionArgs,以简化迁移。

declare module "react-router" {
  // Your AppLoadContext used in v2
  interface AppLoadContext {
    whatever: string;
  }

  // TODO: remove this once we've migrated to `Route.LoaderArgs` instead for our loaders
  interface LoaderFunctionArgs {
    context: AppLoadContext;
  }

  // TODO: remove this once we've migrated to `Route.ActionArgs` instead for our actions
  interface ActionFunctionArgs {
    context: AppLoadContext;
  }
}

export {}; // necessary for TS to treat this as a module

使用 declare module 注册类型是一种标准的 TypeScript 技术,称为模块增强 (module augmentation)。你可以在你的 tsconfig.jsoninclude 字段覆盖的任何 TypeScript 文件中执行此操作,但我们建议在你的应用目录中创建一个专门的 env.ts 文件。

👉 使用新类型

一旦你采用了新类型生成,你可以移除 LoaderFunctionArgs/ActionFunctionArgs 增强,并转而使用来自Route.LoaderArgsRoute.ActionArgscontext 参数。

declare module "react-router" {
  // Your AppLoadContext used in v2
  interface AppLoadContext {
    whatever: string;
  }
}

export {}; // necessary for TS to treat this as a module
import type { Route } from "./+types/my-route";

export function loader({ context }: Route.LoaderArgs) {}
// { whatever: string }  ^^^^^^^

export function action({ context }: Route.ActionArgs) {}
// { whatever: string }  ^^^^^^^

恭喜!你现在已升级到 React Router v7。请运行你的应用,确保一切正常。

文档和示例 CC 4.0