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.
路由器设置(数据模式):
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),
];
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[]
使用 :创建/删除时改变 URL,添加到历史记录
使用 useFetcher:内联更新、列表操作、弹出框——不改变 URL
使用 loader:渲染前获取数据、服务端获取、自动重新验证
使用 useEffect:仅客户端数据、依赖用户交互、订阅
| 特性 | 框架模式 | 数据模式 | 声明式模式 |
|---|---|---|---|
| 设置 | Vite 插件 | createBrowserRouter | <BrowserRouter> |
| 类型安全 |
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 react-router-v7-1775977982 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 react-router-v7-1775977982 技能
skillhub install react-router-v7-1775977982
文件大小: 13.07 KB | 发布时间: 2026-4-13 11:44