Skip to main content

Loaders

Loaders are functions that fetch data for a route before it renders. They’re the backbone of TanStack Router’s data loading strategy, providing type-safe, cacheable, and parallel data fetching.

Why Loaders Matter

Loaders solve critical data loading challenges:
  • Fetch before render - Data loads before components mount
  • Type-safe data flow - Return types automatically inferred
  • Automatic caching - Prevents redundant fetches
  • Parallel loading - Multiple loaders run concurrently
  • Cancellation - Automatic cleanup on navigation away
This eliminates loading states scattered throughout components and provides a better user experience.

Defining Loaders

Loaders are defined using the loader option on routes.

Basic Loader

Loader with Parameters

Path parameters are available in the loader context.

Loader with Context

Access router context in loaders:

Loader Context

Loaders receive a rich context object defined in packages/router-core/src/route.ts:1418-1453:

Available Context Properties

Loader Dependencies

Loader dependencies control when a loader re-runs.

Defining Dependencies

Without loaderDeps, the loader only runs once per route match. Use loaderDeps to re-run loaders when specific values change.

Why Use loaderDeps?

The location object in loader context doesn’t include search params. This is intentional - it encourages you to explicitly declare dependencies:
This ensures caching works correctly.

Accessing Loader Data

Use the useLoaderData hook to access loader results.

In Route Component

In Nested Components

Type-Safe Access

Loader data types are automatically inferred:

Parallel Loaders

Multiple route loaders run in parallel automatically.
Both loaders start simultaneously. The route doesn’t render until all loaders complete.

Awaiting Parent Loader

Sometimes child loaders need parent data:
This creates a sequential dependency when needed.

Loader Cancellation

Loaders are automatically cancelled when navigating away.
Always pass signal to fetch requests for proper cleanup.

Error Handling

Handle loader errors gracefully:
Errors thrown in loaders are caught by the route’s error boundary.

Preloading

Loaders can run before navigation starts:
When preloading, context.preload is true in the loader. See the Prefetching guide for more details.

Conditional Loading

Control whether a loader runs:

shouldReload

Control when cached data should reload:

Integration with React Query

TanStack Router works great with React Query:
This pattern:
  • Ensures data is loaded before render
  • Provides React Query’s caching and refetching
  • Enables mutations and optimistic updates

Loader Best Practices

Pass the abort signal to fetch requests. This prevents memory leaks and unnecessary network requests when users navigate away.
Explicitly declare which search params trigger loader re-runs. This ensures correct caching behavior.
Every route with a loader should have an errorComponent to handle failures gracefully.
Design loaders to be independent. Only use parentMatchPromise when child data truly depends on parent data.
For heavy data loads, split loaders across parent/child routes to show content progressively.

Loader vs beforeLoad

Choose the right lifecycle hook:

Next Steps

Caching

Learn about loader data caching strategies

Prefetching

Optimize performance with loader prefetching

Routes

Explore all route lifecycle options

Search Params

Use search params in loader dependencies