React Router v6 Loaders: Fetch Data Before Render with useLoaderData
Fetch route data before rendering with React Router v6 loaders and useLoaderData, plus handle loader errors with errorElement.
02 Mar 2026, 19:31 UTC

The Problem: Fetching Data After Render Creates Empty States and Waterfalls
In many React applications, data fetching starts inside a component with useEffect. The component renders first, users see a loading spinner, then the request finishes and the component renders again. With nested routes, each level can trigger its own request, creating request waterfalls that slow down page transitions.
The useful takeaway: React Router v6 route loaders fetch data before the route component renders. When the component mounts, the data is already available through useLoaderData, so there is no initial empty state for that route's data.
How Route Loaders Work
A loader is an async function attached to a route definition. React Router calls it during navigation, before rendering the route element. If the loader succeeds, the component receives the result via useLoaderData. If the loader throws, React Router renders the route's errorElement instead and exposes the error through useRouteError.
Worked Route Configuration
This example defines a posts route with a loader, a consuming component, and an error boundary.
import {
createBrowserRouter,
RouterProvider,
useLoaderData,
useRouteError
} from 'react-router-dom';
async function postsLoader() {
const response = await fetch('https://api.example.com/posts');
if (!response.ok) {
throw new Error(`Posts request failed: ${response.status}`);
}
return response.json();
}
function Posts() {
const posts = useLoaderData();
if (!posts) {
return No posts available.;
}
return (
{posts.map((post) => (
- {post.title}
))}
);
}
function PostsError() {
const error = useRouteError();
return (
Could not load posts
{error.message || String(error)}
);
}
const router = createBrowserRouter([
{
path: '/posts',
loader: postsLoader,
element: ,
errorElement:
}
]);
function App() {
return ;
}
Run npm install react-router-dom@6 in the project root. No elevated permissions are required. The command updates package.json and installs dependencies into node_modules. A risk is version mismatch: code written for v6 may not work with v5 or v7 without changes.
Verification Steps
- Start the development server and navigate to
/posts. - Confirm the post list renders without a separate loading spinner for route data.
- Temporarily change
postsLoaderto throw an error beforefetch. - Reload
/postsand confirmPostsErrorrenders instead ofPosts. - Restore the loader and confirm the list returns.
If the error element does not render, check that the loader actually throws. Returning an error object or a rejected promise outside the loader will not trigger errorElement.
Limits and Common Mistakes
Mistake 1: Fetching in useEffect Instead of the Loader
Starting a fetch in useEffect defeats the loader's purpose. The component renders before data arrives, so users see a loading state. Move the fetch into the loader and consume the result with useLoaderData.
Mistake 2: Assuming fetch Rejects on HTTP Errors
fetch resolves on 404 and 500 responses. Always check response.ok and throw when it is false, otherwise the loader succeeds with an error body and errorElement never renders.
Mistake 3: Treating useLoaderData as a Promise
useLoaderData returns the resolved loader value, not a promise. Do not await it or call .then on the result.
Limit: Client-Side Routing Only
createBrowserRouter supports client-side navigation. Server-side rendering requires React Router's server APIs and a different data hydration setup. Loaders in a browser router do not run on the server.
Limit: Loader Errors Are Route-Scoped
An errorElement only catches errors from its own route's loader and component render. Errors in parent route loaders are handled by the nearest ancestor error element, not by every child route.
Key Takeaway
Use React Router v6 loaders to fetch route data before rendering, useLoaderData to read it, and errorElement with useRouteError to handle failures. This removes initial loading states for route data and keeps error handling close to the route that can fail.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.