React Router 是一个用于 React 的多策略路由库。在你的应用中有三种主要的使用方式,或者说“模式”。在整个文档中,你会看到这些图标,它们指示了内容适用于哪种模式
每种模式提供的功能是叠加的,因此从声明式模式转向数据模式再转向框架模式,只需增加更多功能,但会牺牲一些架构控制。因此,根据你想要 React Router 提供多少控制或帮助来选择你的模式。
你使用的模式取决于你使用的是哪个“顶层”路由 API。
声明式
声明式模式启用了基本的路由功能,例如将 URL 匹配到组件、在应用中导航以及使用 <Link>
、useNavigate
和 useLocation
等 API 提供活动状态。
import { BrowserRouter } from "react-router";
ReactDOM.createRoot(root).render(
<BrowserRouter>
<App />
</BrowserRouter>
);
数据
通过将路由配置移到 React 渲染之外,数据模式增加了数据加载、操作、挂起状态等功能,通过 loader
、action
和 useFetcher
等 API 实现。
import {
createBrowserRouter,
RouterProvider,
} from "react-router";
let router = createBrowserRouter([
{
path: "/",
Component: Root,
loader: loadRootData,
},
]);
ReactDOM.createRoot(root).render(
<RouterProvider router={router} />
);
框架
框架模式通过 Vite 插件包装了数据模式,以提供完整的 React Router 体验,包括:
href
import { index, route } from "@react-router/dev/routes";
export default [
index("./home.tsx"),
route("products/:pid", "./product.tsx"),
];
然后,你将能够访问路由模块 API,该 API 具备类型安全的参数、loaderData、代码分割、SPA/SSR/SSG 策略等功能。
import { Route } from "+./types/product.tsx";
export async function loader({ params }: Route.LoaderArgs) {
let product = await getProduct(params.pid);
return { product };
}
export default function Product({
loaderData,
}: Route.ComponentProps) {
return <div>{loaderData.product.name}</div>;
}
每种模式都支持任何架构和部署目标,因此问题不在于你是否想要 SSR、SPA 等,而在于你想要自己做多少。
如果你符合以下条件,请使用框架模式:
如果你符合以下条件,请使用数据模式:
如果你符合以下条件,请使用声明式模式:
<BrowserRouter>
感到满意这主要面向 LLM,但你也可以看看。
API | 框架 | 数据 | 声明式 |
---|---|---|---|
Await | ✅ | ✅ | |
Form | ✅ | ✅ | |
Link | ✅ | ✅ | ✅ |
<Link discover> |
✅ | ||
<Link prefetch> |
✅ | ||
<Link preventScrollReset> |
✅ | ✅ | |
Links | ✅ | ||
Meta | ✅ | ||
NavLink | ✅ | ✅ | ✅ |
<NavLink discover> |
✅ | ||
<NavLink prefetch> |
✅ | ||
<NavLink preventScrollReset> |
✅ | ✅ | |
NavLink isPending |
✅ | ✅ | |
Navigate | ✅ | ✅ | ✅ |
Outlet | ✅ | ✅ | ✅ |
PrefetchPageLinks | ✅ | ||
Route | ✅ | ✅ | ✅ |
Routes | ✅ | ✅ | ✅ |
Scripts | ✅ | ||
ScrollRestoration | ✅ | ✅ | |
ServerRouter | ✅ | ||
usePrompt | ✅ | ✅ | ✅ |
useActionData | ✅ | ✅ | |
useAsyncError | ✅ | ✅ | |
useAsyncValue | ✅ | ✅ | |
useBeforeUnload | ✅ | ✅ | ✅ |
useBlocker | ✅ | ✅ | ✅ |
useFetcher | ✅ | ✅ | |
useFetchers | ✅ | ✅ | |
useFormAction | ✅ | ✅ | |
useHref | ✅ | ✅ | ✅ |
useInRouterContext | ✅ | ✅ | ✅ |
useLinkClickHandler | ✅ | ✅ | ✅ |
useLoaderData | ✅ | ✅ | |
useLocation | ✅ | ✅ | ✅ |
useMatch | ✅ | ✅ | ✅ |
useMatches | ✅ | ✅ | |
useNavigate | ✅ | ✅ | ✅ |
useNavigation | ✅ | ✅ | |
useNavigationType | ✅ | ✅ | ✅ |
useOutlet | ✅ | ✅ | ✅ |
useOutletContext | ✅ | ✅ | ✅ |
useParams | ✅ | ✅ | ✅ |
useResolvedPath | ✅ | ✅ | ✅ |
useRevalidator | ✅ | ✅ | |
useRouteError | ✅ | ✅ | |
useRouteLoaderData | ✅ | ✅ | |
useRoutes | ✅ | ✅ | ✅ |
useSearchParams | ✅ | ✅ | ✅ |
useSubmit | ✅ | ✅ | |
useViewTransitionState | ✅ | ✅ | |
isCookieFunction | ✅ | ✅ | |
isSessionFunction | ✅ | ✅ | |
createCookie | ✅ | ✅ | |
createCookieSessionStorage | ✅ | ✅ | |
createMemorySessionStorage | ✅ | ✅ | |
createPath | ✅ | ✅ | ✅ |
createRoutesStub | ✅ | ✅ | |
createSearchParams | ✅ | ✅ | ✅ |
data | ✅ | ✅ | |
generatePath | ✅ | ✅ | ✅ |
href | ✅ | ||
isCookie | ✅ | ✅ | |
isRouteErrorResponse | ✅ | ✅ | |
isSession | ✅ | ✅ | |
matchPath | ✅ | ✅ | ✅ |
matchRoutes | ✅ | ✅ | ✅ |
parsePath | ✅ | ✅ | ✅ |
redirect | ✅ | ✅ | |
redirectDocument | ✅ | ✅ | |
renderMatches | ✅ | ✅ | ✅ |
replace | ✅ | ✅ | |
resolvePath | ✅ | ✅ | ✅ |