docs/docs/docs/guides/client-loader.en-US.md
Umi provides an out-of-the-box solution for data pre-loading, which can solve the waterfall requests of page components and data dependencies under multi-layer nested routing. Umi automatically initiates their data requests in parallel according to the current route or the route being prepared for transition. Therefore, when the route component is loaded, there is already data available for immediate use.
Configuration to enable:
// .umirc.ts
export default {
clientLoader: {}
}
In the routing file, in addition to the default exported page component, another clientLoader function is exported, and the logic for route data loading is completed within this function.
// pages/.../some_page.tsx
import { useClientLoaderData } from 'umi';
export default function SomePage() {
const { data } = useClientLoaderData();
return <div>{data}</div>;
}
export async function clientLoader() {
const data = await fetch('/api/data');
return data;
}
As shown in the code above, the data returned by the clientLoader function can be obtained inside the component by calling useClientLoaderData.
Consider a scenario with three layers of nested routing:
Such waterfall requests can severely impact the user experience, as shown in the figure below:
If the component's data request is extracted into clientLoader, then Umi can request these data in parallel: