useRouteLoaderData
本页内容

useRouteLoaderData

摘要

参考文档 ↗

通过路由 ID 返回给定路由的 loader 数据。

路由 ID 是自动创建的。它们就是路由文件相对于 app 文件夹的路径,不带扩展名。

路由文件名 路由 ID
app/root.tsx "root"
app/routes/teams.tsx "routes/teams"
app/whatever/teams.$id.tsx "whatever/teams.$id"
import { useRouteLoaderData } from "react-router";

function SomeComponent() {
  const { user } = useRouteLoaderData("root");
}

// You can also specify your own route ID's manually in your routes.ts file:
route("/", "containers/app.tsx", { id: "app" })
useRouteLoaderData("app");

签名

function useRouteLoaderData<T = any>(
  routeId: string,
): SerializeFrom<T> | undefined

参数

routeId

要返回加载器数据的路由 ID

返回

从指定路由的 loader 函数返回的数据,如果未找到则为 undefined

文档和示例 CC 4.0
编辑