Consume route data
A route component needs the result produced by its route loader.
TanStack · Hook
useLoaderData returns loader data from the closest matching route. It supports route-based type narrowing and optional selection of a derived value.
useLoaderData reads the loader result associated with the closest matching route. Pass the route identifier with from for precise inference, optionally transform the value with select, and use strict to control how types are resolved.
useLoaderData({ from?, strict?, select?, structuralSharing? })| Name | Type | Description |
|---|---|---|
from | string | Route id of the closest parent match; optional, but recommended for full type safety. |
strict | boolean | Controls type narrowing; defaults to true. If false, from is ignored and types are loosened. |
select | (loaderData: TLoaderData) => TSelected | Transforms loader data and returns the transformed value. |
structuralSharing | boolean | Controls structural sharing for the value returned by select. |
The selected value when select is provided; otherwise the route's loader data, or loosened loader data when strict is false.
import { createFileRoute } from '@tanstack/react-router'
export const Route = createFileRoute('/posts/$postId')({
loader: async ({ params }) => ({
postId: params.postId,
title: 'Example post',
}),
component: Post,
})
function Post() {
const data = Route.useLoaderData()
return <h1>{data.title}</h1>
}The route loader returns data, and the route component reads that result with the route-scoped hook.
A route component needs the result produced by its route loader.
A component needs only one field or a derived projection of a larger loader result.
A shared component reads loader data while preserving or intentionally relaxing route-specific typing.
It returns loader data from the closest RouteMatch in the component tree. With a select function, it returns the selected result instead of the complete loader value.
Follow-up: How can a component obtain precise types? Pass the relevant route id through from; this is optional but recommended for full type safety.
The hook accepts an options object containing from, strict, select, and structuralSharing. from identifies the closest parent match, while select transforms the loader data before it is returned.
Follow-up: What does select receive? It receives the loader-data value and its return value becomes the hook result.
strict defaults to true. When strict is false, from is ignored and the returned type is loosened to account for the shared types of possible loader data.
Follow-up: When is strict false useful? It can be used when a component intentionally consumes loader data without narrowing to one route match.
Route.useLoaderDataloaderRouteMatchgetRouteApi