React Router
React Router 是依模式而定的。在修改應用程式之前,請先識別其模式,載入對應的參考文件,然後閱讀已安裝套件版本的說明文件。
識別模式
除非你打算進行模式遷移,否則不要將 Framework/Data 模式套用至 Declarative 應用程式。
Framework 模式
當你看到以下情況時,請使用 Framework 模式指引:
@react-router/dev在 dependencies 中react-router.config.tsapp/routes.tsapp/entry.server.tsx和/或app/entry.client.tsx檔案app/routes/下的路由模組- 路由匯出如
loader、action、clientLoader、clientAction、ErrorBoundary、meta、links或headers - 從
./+types/...匯入 - 來自
@react-router/dev/vite的 React Router Vite 外掛
Framework 範例通常使用預設的 app/ 目錄,但在假設確切路徑之前,請檢查 react-router.config.ts 是否有自訂的 appDirectory。
然後閱讀 references/framework-mode.md。
Data 模式
當你看到以下情況時,請使用 Data 模式指引:
createBrowserRouter、createHashRouter、createMemoryRouter或createStaticRouter<RouterProvider router={router}>- 路由物件,其屬性如
path、children、loader、action、Component、ErrorBoundary或lazy - 沒有 Framework Vite 外掛的資料 API
然後閱讀 references/data-mode.md。
Declarative 模式
當你看到以下情況時,請使用 Declarative 模式指引:
<BrowserRouter>、<HashRouter>或<MemoryRouter><Routes>和<Route>JSX 路由設定- 使用
element={<Component />}傳遞的路由組件 - 沒有資料路由器、沒有路由模組慣例,也沒有 loaders/actions
然後閱讀 references/declarative-mode.md。
RSC Framework 與 RSC Data 模式
React Server Components 的支援尚不穩定,並且同時存在於 Framework 和 Data 變體中。當你看到以下情況時,請使用 RSC 指引:
unstable_reactRouterRSC@vitejs/plugin-rscunstable_RSCRouteConfig- RSC 入口檔案,例如
entry.rsc ServerComponent、ServerErrorBoundary、ServerLayout或ServerHydrateFallback- React 指令或邊界套件,例如
"use client"、"server-only"或"client-only"
對於 RSC Framework,請同時閱讀 references/framework-mode.md 和 references/rsc.md。
對於 RSC Data,請同時閱讀 references/data-mode.md 和 references/rsc.md。
以已安裝文件作為唯一真相來源
React Router 在套件中提供了 markdown 文件,以便指引能與已安裝版本匹配:
node_modules/react-router/docs/
關鍵文件路徑:
node_modules/react-router/docs/index.md
node_modules/react-router/docs/start/
node_modules/react-router/docs/how-to/
node_modules/react-router/docs/explanation/
node_modules/react-router/docs/upgrading/
當此技能參考 react-router/docs/... 時,請閱讀 node_modules/react-router/docs/ 下的對應檔案。如果已安裝版本不包含本地文件,請在 React Router 儲存庫內部工作時使用儲存庫的 docs/ 目錄;在消費應用程式中,請回退到與版本匹配的網站文件。
大多數文件在頂部附近包含一個模式標記:
[MODES: framework, data, declarative]
僅當文件的模式標記與應用程式模式匹配時,才套用該文件。如果任務跨越多種模式,請優先選擇與當前應用程式匹配的章節或檔案。
RSC 主要記錄在:
node_modules/react-router/docs/how-to/react-server-components.md
技能參考
在識別模式後載入相關參考:
| 參考 | 使用時機 |
|---|---|
references/framework-mode.md | Framework 模式或 RSC Framework 基礎行為 |
references/data-mode.md | Data 模式或 RSC Data 基礎行為 |
references/declarative-mode.md | Declarative 模式 |
references/rsc.md | 任何不穩定的 RSC 應用程式 |
模式遷移文件索引
如果使用者明確要求切換模式,請閱讀目標模式參考以及與遷移相關的文件:
| 遷移 | 需閱讀的文件 |
|---|---|
| Declarative → Data | react-router/docs/start/modes.md、react-router/docs/start/data/routing.md、react-router/docs/start/data/data-loading.md、react-router/docs/start/data/actions.md |
| Declarative/Data → Framework | react-router/docs/start/modes.md、react-router/docs/start/framework/routing.md、react-router/docs/start/framework/route-module.md、react-router/docs/how-to/route-module-type-safety.md |
| Framework SPA/SSR/預渲染變更 | react-router/docs/start/framework/rendering.md、react-router/docs/how-to/spa.md、react-router/docs/how-to/pre-rendering.md、react-router/docs/start/framework/data-loading.md、react-router/docs/start/framework/actions.md |
| 未來旗標/升級 | react-router/docs/upgrading/future.md 以及 react-router/docs/upgrading/ 下的相關檔案 |