Fetch Data Before Render with React Router Route Loaders
React Router v6.4+ route loaders resolve data before components mount, removing manual loading flags and useEffect fetch chains from product pages and profiles.
24 Jul 2026, 03:36 UTC

The empty-screen problem
Many React pages still fetch data inside useEffect. The component mounts, shows a skeleton or empty state, starts the request, and only then renders the result. That sequence creates a visible waterfall: navigation completes, the shell paints, and the data arrives a moment later. On slower networks, users see a blank product card or profile before anything useful appears.
It also forces every component to manage its own loading and error flags. If two routes need the same data, you duplicate the fetch logic and the loading state in two places.
What a route loader changes
React Router v6.4 introduced route loader functions. A loader runs after the router matches a route but before the route component mounts. When the loader resolves, the router passes the result to the component through the useLoaderData hook. Because data resolution happens before render, the component can assume the data exists instead of conditionally rendering a loading state.
The router also tracks whether a loader is in flight through its navigation state. The useNavigation hook can show a global loading indicator without you maintaining a manual isFetching flag.
Worked example: product detail page
Suppose you have a product page at /products/:productId. Instead of fetching inside the component, define a loader in the route config:
import {
createBrowserRouter,
RouterProvider,
} from "react-router-dom";
async function productLoader({ params }) {
const response = await fetch(
`https://api.example.com/products/${params.productId}`
);
if (!response.ok) {
throw new Error("Product not found");
}
return response.json();
}
const router = createBrowserRouter([
{
path: "/products/:productId",
element: ,
loader: productLoader,
errorElement: ,
},
]);
export default function App() {
return ;
}
The component reads the resolved value with useLoaderData:
import { useLoaderData } from "react-router-dom";
export default function ProductPage() {
const product = useLoaderData();
return (
{product.name}
{product.description}
);
}
Run npm list react-router-dom in your project root before using this pattern. You need React Router DOM 6.4.0 or later; earlier v6 releases do not support route loaders on createBrowserRouter. No special permissions are required for the command, but an outdated version will cause a runtime error when the router encounters the loader prop.
Show loading state and handle failures
Use useNavigation to indicate that a navigation is waiting on a loader:
import { useNavigation } from "react-router-dom";
export function GlobalLoadingBar() {
const navigation = useNavigation();
if (navigation.state === "loading") {
return Loading...;
}
return null;
}
If a loader throws, React Router renders the nearest errorElement instead of the route component. In the example above, a failed product fetch renders ProductError. This gives you a supported failure path without wrapping every component in its own try/catch.
Trade-offs and limitations
- Loaders block the new route from rendering until they resolve. A slow loader delays the navigation, so keep them focused on data resolution and avoid side effects.
- Treat loader return values as read-only. Mutating them inside a component can cause confusing re-render behavior.
- This pattern requires React Router v6.4+. Framework wrappers such as Remix may execute loaders differently; confirm behavior for your deployment target.
Verify before you ship
- Run
npm list react-router-domand confirm the version is 6.4.0 or newer. - Add a
console.log("loader ran")statement inside the loader, navigate to the route, and confirm the log appears before the component mounts. - Throw an error inside the loader and confirm the nearest
errorElementrenders instead of the component tree.
Route loaders move data fetching out of the component lifecycle and into the route definition. For read-first pages such as product details or user profiles, that means less boilerplate, fewer loading flags, and a UI that paints only when the required data is ready.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.