router-core/data-loading

Implement route loaders, SWR caching, and deferred data loading in TanStack Router applications.

1.4k|34|Updated Mar 7, 2024
One-click install
npx skills add https://github.com/mehdibha/dotUI --skill router-core-data-loading-mehdibha
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: router-core/data-loading
Source: https://github.com/mehdibha/dotUI/tree/main/.claude/skills/tanstack-router-data-loading
Command: npx skills add https://github.com/mehdibha/dotUI --skill router-core-data-loading-mehdibha

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires @tanstack/react-router.

What problem does it solve? Fetching and caching route data in TanStack Router involves subtle behaviors — loaders run on the client by default, staleTime defaults to 0, and context injection requires a factory pattern — that commonly cause bugs like unnecessary refetches, broken error retries, and TypeScript inference failures. ## Core Features & Use Cases - Route Loaders & Cache Keys: Configure loader functions with loaderDeps so only relevant search params trigger reloads, and tune staleTime/gcTime for SWR caching. - Pending, Error & Deferred States: Use pendingComponent with pendingMs/pendingMinMs, errorComponent with router.invalidate() retries, and the Await component for deferred promises. - Router Context DI: Inject auth and API clients into loaders via createRootRouteWithContext and RouterProvider context instead of calling React hooks inside loaders. - Use Case: When building a paginated posts page, declare loaderDeps for offset/limit, set a staleTime to avoid background refetches, and invalidate the router after a mutation to refresh the list. ## Quick Start Show me how to add a loader with search-param cache keys, a pending component, and an error component to my TanStack Router posts route.

Frequently Asked Questions about router-core/data-loading

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I load data in a TanStack Router route?▼

Define a loader function on the route that returns your data, then consume it in the component with Route.useLoaderData() or getRouteApi for code-split files. The loader receives params, deps, context, and an abortController for cancellation.

How do loaderDeps and search params work in TanStack Router?▼

loaderDeps declares which validated search params form the loader cache key, and those values are passed to the loader as deps. Return only the params that affect data fetching, since returning the whole search object triggers reloads on any param change.

Do TanStack Router loaders run on the server?▼

Loaders run on the client by default because TanStack Router is client-first. They also run on the server when using TanStack Start for SSR, but server-only code like fs or database queries requires Start server functions.

Why does my TanStack Router loader refetch on every navigation?▼

The default staleTime is 0, so cached data is always considered stale and reloads in the background on every route re-match. Set a staleTime in milliseconds on the route or defaultStaleTime on the router to keep data fresh longer.

How do I retry a failed loader in TanStack Router?▼

Call router.invalidate() from your errorComponent instead of using reset(). The reset function only clears the error boundary UI, while invalidate re-runs the loaders and resets the boundary together.

Can I use React hooks inside a TanStack Router loader?▼

No, loaders and beforeLoad are not React components, so hooks will crash. Call the hook in a component above RouterProvider and pass the value through the router context, then read it from the loader's context argument.