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
Defining Loaders
Loaders are defined using theloader option on routes.
Basic Loader
Loader with Parameters
Loader with Context
Access router context in loaders:Loader Context
Loaders receive a rich context object defined inpackages/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?
Thelocation object in loader context doesn’t include search params. This is intentional - it encourages you to explicitly declare dependencies:
Accessing Loader Data
Use theuseLoaderData 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.Awaiting Parent Loader
Sometimes child loaders need parent data:Loader Cancellation
Loaders are automatically cancelled when navigating away.signal to fetch requests for proper cleanup.
Error Handling
Handle loader errors gracefully:Preloading
Loaders can run before navigation starts: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:- Ensures data is loaded before render
- Provides React Query’s caching and refetching
- Enables mutations and optimistic updates
Loader Best Practices
Always use abortController.signal
Always use abortController.signal
Pass the abort signal to fetch requests. This prevents memory leaks and unnecessary network requests when users navigate away.
Use loaderDeps for search params
Use loaderDeps for search params
Explicitly declare which search params trigger loader re-runs. This ensures correct caching behavior.
Handle errors with errorComponent
Handle errors with errorComponent
Every route with a loader should have an
errorComponent to handle failures gracefully.Leverage parallel loading
Leverage parallel loading
Design loaders to be independent. Only use
parentMatchPromise when child data truly depends on parent data.Consider loader splitting
Consider loader splitting
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