Skip to main content

Data Loading

还没翻译译自英文(reactrouter.com)本页 9 段都还没译

Data Loading

Data

Providing Data

Data is provided to route components from route loaders:

createBrowserRouter([
  {
    path: "/",
    loader: async () => {
      // return data from here
      return { records: await getSomeRecords() };
    },
    Component: MyRoute,
  },
]);

Accessing Data

The data is available in route components with useLoaderData.

import { useLoaderData } from "react-router";

function MyRoute() {
  const { records } = useLoaderData();
  return <div>{records.length}</div>;
}

As the user navigates between routes, the loaders are called before the route component is rendered.


Next: Actions

Traducción © 2026 fanyi.dev · Original © 2026 React Training LLC, Remix Software Inc., Shopify Inc. (MIT)