返回顶部
r

react-router-v7React路由v7

React Router v7 best practices for data-driven routing. Use when implementing routes, loaders, actions, Form components, fetchers, navigation guards, protected routes, or URL search params. Triggers on createBrowserRouter, RouterProvider, useLoaderData, useActionData, useFetcher, NavLink, Outlet.

作者: admin | 来源: ClawHub
源自
ClawHub
版本
V 1.1.0
安全检测
已通过
97
下载量
免费
免费
0
收藏
概述
安装方式
版本历史

react-router-v7

React Router v7 最佳实践

快速参考

路由器设置(数据模式)
tsx
import { createBrowserRouter, RouterProvider } from react-router;

const router = createBrowserRouter([
{
path: /,
Component: Root,
ErrorBoundary: RootErrorBoundary,
loader: rootLoader,
children: [
{ index: true, Component: Home },
{ path: products/:productId, Component: Product, loader: productLoader },
],
},
]);

ReactDOM.createRoot(root).render();

框架模式(Vite 插件)
ts
// routes.ts
import { index, route } from @react-router/dev/routes;

export default [
index(./home.tsx),
route(products/:pid, ./product.tsx),
];

路由配置

嵌套路由与 Outlet

tsx
createBrowserRouter([
{
path: /dashboard,
Component: Dashboard,
children: [
{ index: true, Component: DashboardHome },
{ path: settings, Component: Settings },
],
},
]);

function Dashboard() {
return (


仪表盘


{/ 渲染子路由 /}

);
}

动态段与通配符

tsx
{ path: teams/:teamId } // params.teamId
{ path: :lang?/categories } // 可选段
{ path: files/ } // 通配符:params[]

关键决策点

Form 与 Fetcher

使用

:创建/删除时改变 URL,添加到历史记录
使用 useFetcher:内联更新、列表操作、弹出框——不改变 URL

Loader 与 useEffect

使用 loader:渲染前获取数据、服务端获取、自动重新验证
使用 useEffect:仅客户端数据、依赖用户交互、订阅

附加文档

模式对比

特性框架模式数据模式声明式模式
设置Vite 插件createBrowserRouter<BrowserRouter>
类型安全
自动生成类型 | 手动 | 手动 | | SSR 支持 | 内置 | 手动 | 有限 | | 使用场景 | 全栈应用 | 可控 SPA | 简单/遗留项目 |

标签

skill ai

通过对话安装

该技能支持在以下平台通过对话安装:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 react-router-v7-1775977982 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 react-router-v7-1775977982 技能

通过命令行安装

skillhub install react-router-v7-1775977982

下载

⬇ 下载 react-router-v7 v1.1.0(免费)

文件大小: 13.07 KB | 发布时间: 2026-4-13 11:44

v1.1.0 最新 2026-4-13 11:44
- Added comprehensive best practices and quick reference for React Router v7 usage.
- Expanded documentation on route setup, nested routes, dynamic segments, and splats.
- Clarified when to use Forms vs. fetchers, and loader vs. useEffect for data handling.
- Included a mode comparison table covering Framework, Data, and Declarative modes.
- Linked to additional references for data loading, mutations, navigation, and advanced features.

Archiver·手机版·闲社网·闲社论坛·羊毛社区· 多链控股集团有限公司 · 苏ICP备2025199260号-1

Powered by Discuz! X5.0   © 2024-2025 闲社网·线报更新论坛·羊毛分享社区·http://xianshe.com

p2p_official_large
返回顶部