Course contentsShow
← React
Lesson 1846 of 2,39643. RoutingFree lesson

Accessing Loader Data with useLoaderData

Retrieving type-safe data from loaders in components without useEffect or loading states.

Accessing Loader Data with useLoaderData

What you'll learn: How to retrieve type-safe data from route loaders directly in your components without useEffect or loading states.

The Core Idea

Once a route loader has fetched data (as you learned in the previous lesson), you need a way to access that data inside your component. The useLoaderData hook provides exactly this—it returns whatever your loader resolved with, no async handling required.

This is the payoff of the Data Router paradigm: your component renders after the data is ready, so there's no loading spinner needed, no useEffect to trigger a fetch, and no null checks for data that "might" arrive. The router guarantees the loader completed successfully before your component mounts.

How It Works

Call useLoaderData() at the top of your route component. It returns the exact value your loader returned:

// Route definition (from previous lesson)
{
  path: '/users/:id',
  loader: async ({ params }) => {
    const res = await fetch(`/api/users/${params.id}`);
    return res.json(); // { name: 'Alice', email: '...' }
  },
  element: <UserProfile />
}

// UserProfile.tsx
function UserProfile() {
  const user = useLoaderData(); // { name: 'Alice', email: '...' }
  
  return <h1>{user.name}</h1>; // Renders immediately with data
}

No useState, no useEffect, no if (!user) return <Spinner />. The data is simply there.

Type Safety

When using TypeScript, cast or infer the loader return type for full type safety throughout your component.

Key Takeaway: useLoaderData gives you instant, guaranteed access to your route's loader data, eliminating async boilerplate and making components cleaner and more declarative.