React Router Data Mode

remix-run/agent-skills/skills/react-router-data-mode

作者 remix-run33578aae4aa3MIT135 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫3 個月前更新

Build React applications using React Router's data mode with createBrowserRouter and RouterProvider. Use when working with route objects, loaders, actions, Form, useFetcher, or pending/optimistic UI without the Vite plugin.

已封存僅含說明Software Development
AI 產生的概覽

指導使用 React Router 資料模式建置 React 應用程式,涵蓋 createBrowserRouter、loader、action 與待處理 UI。

功能
此技能提供使用 React Router 資料模式建置 React 應用程式的說明,核心是不需 Vite 外掛即可使用 createBrowserRouter 與 RouterProvider。內容涵蓋路由物件、loader、action、導覽、useFetcher 以及待處理或樂觀 UI,並指向七個參考檔案以取得各主題的詳細指引。它產出的是實作指引與程式碼模式,而非可直接執行的成品。
適用情境
在為現有 React 應用程式加入資料載入與資料變更能力並使用 React Router 資料模式時使用。適用於涉及路由物件、loader、action、Form、useFetcher 或待處理與樂觀 UI 狀態的工作。
執行需求
不包含指令碼,僅為說明文件與參考文件。所述方法以使用 react-router 套件的 React 為對象,技能說明可線上搜尋官方文件。

React Router Data Mode

Data mode uses createBrowserRouter and RouterProvider to enable data loading, actions, and pending UI without the framework's Vite plugin. This is ideal for existing React applications that want to add data loading and mutation capabilities.

When to Apply

  • Using createBrowserRouter with route objects
  • Loading data with loader property on routes
  • Handling mutations with action property
  • Navigating with <Link>, <NavLink>, <Form>, redirect, and useNavigate
  • Implementing pending/loading UI states with useNavigation
  • Using useFetcher for mutations without navigation

References

Load the relevant reference for detailed guidance on the specific API/concept:

ReferenceUse When
references/routing.mdConfiguring routes, nested routes, layout
references/route-object.mdUnderstanding route object properties
references/data-loading.mdLoading data with loaders
references/actions.mdHandling forms, mutations, validation
references/navigation.mdLinks, programmatic navigation, redirects
references/pending-ui.mdLoading states, optimistic UI
references/ssr.mdServer-side rendering with data mode

Critical Patterns

These are the most important patterns to follow. Load the relevant reference for full details.

Basic Router Setup

tsx
import { createBrowserRouter, RouterProvider } from "react-router";
const router = createBrowserRouter([  {    path: "/",    Component: Root,    children: [      { index: true, Component: Home },      { path: "about", Component: About },    ],  },]);
ReactDOM.createRoot(root).render(<RouterProvider router={router} />);

Forms & Mutations

Search forms - use <Form method="get">, NOT onSubmit with setSearchParams:

tsx
// ✅ Correct<Form method="get">  <input name="q" /></Form>
// ❌ Wrong - don't manually handle search params<form onSubmit={(e) => { e.preventDefault(); setSearchParams(...) }}>

Inline mutations - use useFetcher, NOT <Form> (which causes page navigation):

tsx
const fetcher = useFetcher();const optimistic = fetcher.formData  ? fetcher.formData.get("favorite") === "true"  : isFavorite;
<fetcher.Form method="post" action={`/favorites/${id}`}>  <button>{optimistic ? "★" : "☆"}</button></fetcher.Form>;

See references/actions.md for complete patterns.

Optimistic UI Pattern

Use fetcher.formData to show expected results immediately:

tsx
function FavoriteButton({ itemId, isFavorite }) {  const fetcher = useFetcher();
  // Optimistic: use pending form data, fallback to server state  const optimistic = fetcher.formData    ? fetcher.formData.get("favorite") === "true"    : isFavorite;
  return (    <fetcher.Form method="post" action={`/items/${itemId}/favorite`}>      <input type="hidden" name="favorite" value={String(!optimistic)} />      <button>{optimistic ? "★" : "☆"}</button>    </fetcher.Form>  );}

See references/pending-ui.md for complete patterns.

Further Documentation

If anything related to React Router is not covered in these references, you can search the official documentation:

https://reactrouter.com/docs

來源與署名

來源:remix-run/agent-skills位於skills/react-router-data-mode提交33578aa

授權條款: MIT

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架