Load route data before rendering with React Router loaders
Use React Router v6.4+ route loaders to fetch data before a route renders, then read it with useLoaderData.
30 Oct 2025, 12:01 UTC

Desired outcome
When a user navigates to a route, the page should not show a loading spinner inside the route component while data fetches after mount. Instead, the router should resolve the route's data first, then render the component with the data already available.
Useful takeaway: In React Router v6.4 and later, define a loader on a route and read its return value with useLoaderData. The router waits for the loader to settle before rendering the matched route.
Prerequisites
- React Router version 6.4.0 or higher. Loaders do not exist in version 5.
- A router created with
createBrowserRouteror a similar router builder, not justBrowserRouteralone. - Route objects with
element,loader, and optionalerrorElementproperties.
Define and wire a loader
A loader is an async function that runs when the router enters a route. It can return plain data, a promise, or a Response instance. Throwing a Response renders the route's errorElement instead of an uncaught exception.
import { createBrowserRouter } from "react-router-dom";
async function postsLoader() {
const response = await fetch("https://api.example.com/posts");
if (!response.ok) {
throw new Response("Failed to load posts", { status: response.status });
}
return response.json();
}
export const router = createBrowserRouter([
{
path: "/",
element: <Layout />,
children: [
{
path: "posts",
element: <PostsList />,
loader: postsLoader,
errorElement: <PostsError />,
},
],
},
]);
Run npm ls react-router-dom in your project terminal to confirm the installed version. This is a read-only command and requires no special permissions. If the version is below 6.4.0, upgrade before using route loaders.
Read loader data in a route component
Inside the route component, call useLoaderData with no arguments. It returns the resolved value from the matching route's loader.
import { useLoaderData } from "react-router-dom";
function PostsList() {
const posts = useLoaderData();
return (
<ul>
{posts.map((post) => (
<li key={post.id}>{post.title}</li>
))}
</ul>
);
}
The component does not need a useEffect or a local loading state for this data. Because the router delays rendering until the loader settles, posts is available on first render.
Handle loader errors
If a loader throws a Response, React Router renders the route's errorElement and does not mount the normal route element. Use this for failed requests, missing records, or authorization failures.
function PostsError() {
return <p>Could not load posts.</p>;
}
Throwing anything other than a Response can produce an unhandled error. Keep loader error throws consistent.
Loader versus component-level fetching
| Approach | Data available | Loading state | Error boundary |
|---|---|---|---|
useEffect fetch in component | After component mounts | Inside the component | try/catch in the effect |
| Route loader | Before route renders | Router-level pending UI | errorElement on the route |
Component-level fetching creates a waterfall: navigation completes, the component mounts, then the request starts. A loader starts the request during navigation, so the transition can wait for data instead of rendering an empty shell.
Verify behavior and diagnose common issues
Add temporary log statements to confirm execution order:
async function postsLoader() {
console.log("[loader] started");
const response = await fetch("https://api.example.com/posts");
console.log("[loader] resolved");
return response.json();
}
function PostsList() {
const posts = useLoaderData();
console.log("[component] mounted", posts);
return <div>...</div>;
}
Expected checks:
- On navigation to
/posts, the console shows "[loader] started" before "[component] mounted". - The value printed by
useLoaderDatamatches the object returned by the loader. - Reloading the page runs the loader again for the initial route, according to your server or build setup.
- Navigating away from
/postsand back re-runs the loader for that route; unchanged parent layout loaders do not re-run on every child navigation.
If the component renders with undefined data, verify the loader is attached to the same route object that renders the component, not a parent route with a different path.
Limitations and rollback
- Loaders are unavailable in React Router v5. Migrating requires replacing component-level data fetching with the route data API.
- A slow loader blocks the route transition until it settles. For large or slow payloads, use deferred data with
deferandAwaitrather than blocking the entire navigation. - Loaders are for read-only data. Use
actionoruseFetcherfor writes. - Authentication checks in a loader should be paired with UI guards or layout routes to prevent flashing unauthorized content before a redirect.
Rollback: remove the loader property from the route, restore any previous useEffect fetch logic in the component, and delete useLoaderData usage. If the changes are committed, revert the commit. This restores the prior component-level fetching behavior.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.