DocsPluginsShowcaseReleases
npmStar us on GitHub

Getting Started

IntroductionInstallationProject StructureLayouts and PagesLinking and Navigating

Routing

Folder-Based RoutingFile-Based RoutingDynamic RoutesParallel RoutesIntercepting RoutesCatch-All RoutesMDX & Markdown Pages

Special Files

Loading UIError BoundariesTemplateDefaultNot Found

Data Fetching

LoadersActionsAccessing Route Data

Metadata

Metadata & SEOOpen Graph & TwitterIcons & Favicons

API Routes

API Routes OverviewPlain Function HandlersHono IntegrationDynamic API Routes

Environment Variables

OverviewPrefixes & Client ExposureUsing in API Routes

Styling

CSS OverviewTailwind CSSCSS Modules

Platforms

WebWindowsmacOSLinuxAndroidiOS

Deployment

Deployment OverviewProduction ServerStatic ExportHosting Providers

A native React framework for building cross-platform applications from a single codebase.

Resources

  • Documentation
  • Plugins
  • Showcase
  • Releases

Community

  • GitHub Discussions
  • Reddit
  • Discord
  • LinkedIn

More

  • npm
  • GitHub
  • Issues
  • License

© 2026 Bini Cooperation.

Loaders

Experimental

Fetch data before a route renders with loader exports and useLoaderData().

Overview

A loader is a function exported from a page that fetches data before the page renders. React Router runs it, resolves the result, and hands it to the page via useLoaderData().

app
users
page.tsx
/users
ExportTypeWhen it runs
loaderfunction (async or sync)Before the route renders
actionfunction (async or sync)On form submission or POST/PUT/DELETE
shouldRevalidatefunctionBefore a revalidation
handleobjectStatic route metadata for useMatches()
Data exports are detected statically at build time. A page with a loader is marked clientOnly and gets a shell page during static generation instead of server-rendered HTML.

Creating a Loader

Export a function called loader from your page. It can be sync or async. Return any serializable value.

src/app/users/page.tsx
export async function loader() {
  const res = await fetch('/api/users')
  return res.json()
}

export default function Users() {
  const users = useLoaderData()
  return (
    <ul>
      {users.map((u) => (
        <li key={u.id}>{u.name}</li>
      ))}
    </ul>
  )
}

Accessing Loader Data

Call useLoaderData() inside the page component to access the resolved value.

HookWhat it returns
useLoaderData()The route's loader return value
useRouteLoaderData(id)Another route's loader value by route id
useNavigation()The current navigation state (idle, loading, submitting)

useLoaderData() is auto-imported from react-router-dom inside files under src/app/. In MDX files you must import it explicitly.

Dynamic Params in Loaders

The loader function receives an object containing the request and the route params. You can read dynamic segments directly inside the loader before fetching data.

src/app/blog/[slug]/page.tsx
export async function loader({ params }: { params: { slug: string } }) {
  const res = await fetch(`/api/posts/${params.slug}`)
  if (!res.ok) throw new Error('Post not found')
  return res.json()
}

export default function BlogPost() {
  const post = useLoaderData()
  return <h1>{post.title}</h1>
}
For catch-all routes like [...slug], the value is exposed under params['*'], not the name written in the folder.

Error Handling

A thrown error inside a loader is caught by the nearest error.tsx boundary in the segment chain. Use the same error UI you would use for render errors.

src/app/users/page.tsx
export async function loader() {
  const res = await fetch('/api/users')
  if (!res.ok) {
    throw new Error('Failed to load users')
  }
  return res.json()
}

Client-only Routes

Routes with data exports are marked clientOnly. During vite build, the build tool writes a shell page for them instead of a server-rendered page, because React Router does not run lazy loaders during static rendering.

Route typeSSG outputWhat renders on first visit
Static, no loaderFull pre-rendered HTMLInstant paint from HTML
Static, with loaderShell page (empty #root)Loading UI, then client render
Dynamic, with loaderShell page (empty #root)Loading UI, then client render
To get real HTML for a route with a loader, list its URLs in staticParams. Those URLs are rendered even when the route is client-only.

Complete Example

app
users
layout.tsx
page.tsx
error.tsx
[id]
page.tsx
/users
/users/:id
src/app/users/[id]/page.tsx
export async function loader({ params }: { params: { id: string } }) {
  const res = await fetch(`/api/users/${params.id}`)
  if (!res.ok) throw new Error('User not found')
  return res.json()
}

export default function UserDetail() {
  const user = useLoaderData()
  return (
    <div>
      <h1>{user.name}</h1>
      <p>{user.email}</p>
    </div>
  )
}

Actions

Handle form submissions and mutations.

Accessing Route Data

Access loader data and navigation state.

Error Boundaries

Catch render errors and show a fallback UI.

Loading UI

Show a fallback while pages and layouts load.

Dynamic Routes

Match URL segments with dynamic parameters and catch-alls.

Static Export

How every route is pre-rendered to HTML at build time.

Was this helpful?
PreviousNot Found (404)
NextActions

On this page

  • Overview
  • Creating a Loader
  • Accessing Loader Data
  • Dynamic Params in Loaders
  • Error Handling
  • Client-only Routes
  • Complete Example
Edit this page on GitHub

Loaders

Experimental

Fetch data before a route renders with loader exports and useLoaderData().

Overview

A loader is a function exported from a page that fetches data before the page renders. React Router runs it, resolves the result, and hands it to the page via useLoaderData().

app
users
page.tsx
/users
ExportTypeWhen it runs
loaderfunction (async or sync)Before the route renders
actionfunction (async or sync)On form submission or POST/PUT/DELETE
shouldRevalidatefunctionBefore a revalidation
handleobjectStatic route metadata for useMatches()
Data exports are detected statically at build time. A page with a loader is marked clientOnly and gets a shell page during static generation instead of server-rendered HTML.

Creating a Loader

Export a function called loader from your page. It can be sync or async. Return any serializable value.

src/app/users/page.tsx
export async function loader() {
  const res = await fetch('/api/users')
  return res.json()
}

export default function Users() {
  const users = useLoaderData()
  return (
    <ul>
      {users.map((u) => (
        <li key={u.id}>{u.name}</li>
      ))}
    </ul>
  )
}

Accessing Loader Data

Call useLoaderData() inside the page component to access the resolved value.

HookWhat it returns
useLoaderData()The route's loader return value
useRouteLoaderData(id)Another route's loader value by route id
useNavigation()The current navigation state (idle, loading, submitting)

useLoaderData() is auto-imported from react-router-dom inside files under src/app/. In MDX files you must import it explicitly.

Dynamic Params in Loaders

The loader function receives an object containing the request and the route params. You can read dynamic segments directly inside the loader before fetching data.

src/app/blog/[slug]/page.tsx
export async function loader({ params }: { params: { slug: string } }) {
  const res = await fetch(`/api/posts/${params.slug}`)
  if (!res.ok) throw new Error('Post not found')
  return res.json()
}

export default function BlogPost() {
  const post = useLoaderData()
  return <h1>{post.title}</h1>
}
For catch-all routes like [...slug], the value is exposed under params['*'], not the name written in the folder.

Error Handling

A thrown error inside a loader is caught by the nearest error.tsx boundary in the segment chain. Use the same error UI you would use for render errors.

src/app/users/page.tsx
export async function loader() {
  const res = await fetch('/api/users')
  if (!res.ok) {
    throw new Error('Failed to load users')
  }
  return res.json()
}

Client-only Routes

Routes with data exports are marked clientOnly. During vite build, the build tool writes a shell page for them instead of a server-rendered page, because React Router does not run lazy loaders during static rendering.

Route typeSSG outputWhat renders on first visit
Static, no loaderFull pre-rendered HTMLInstant paint from HTML
Static, with loaderShell page (empty #root)Loading UI, then client render
Dynamic, with loaderShell page (empty #root)Loading UI, then client render
To get real HTML for a route with a loader, list its URLs in staticParams. Those URLs are rendered even when the route is client-only.

Complete Example

app
users
layout.tsx
page.tsx
error.tsx
[id]
page.tsx
/users
/users/:id
src/app/users/[id]/page.tsx
export async function loader({ params }: { params: { id: string } }) {
  const res = await fetch(`/api/users/${params.id}`)
  if (!res.ok) throw new Error('User not found')
  return res.json()
}

export default function UserDetail() {
  const user = useLoaderData()
  return (
    <div>
      <h1>{user.name}</h1>
      <p>{user.email}</p>
    </div>
  )
}

Actions

Handle form submissions and mutations.

Accessing Route Data

Access loader data and navigation state.

Error Boundaries

Catch render errors and show a fallback UI.

Loading UI

Show a fallback while pages and layouts load.

Dynamic Routes

Match URL segments with dynamic parameters and catch-alls.

Static Export

How every route is pre-rendered to HTML at build time.

Was this helpful?
PreviousNot Found (404)
NextActions

On this page

  • Overview
  • Creating a Loader
  • Accessing Loader Data
  • Dynamic Params in Loaders
  • Error Handling
  • Client-only Routes
  • Complete Example
Edit this page on GitHub