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 从公开仓库中收录这些内容。

举报或申请下架