Fetch data before a route renders with loader exports and useLoaderData().
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().
| Export | Type | When it runs |
|---|---|---|
| loader | function (async or sync) | Before the route renders |
| action | function (async or sync) | On form submission or POST/PUT/DELETE |
| shouldRevalidate | function | Before a revalidation |
| handle | object | Static route metadata for useMatches() |
clientOnly and gets a shell page during static generation instead of server-rendered HTML.Export a function called loader from your page. It can be sync or async. Return any serializable value.
Call useLoaderData() inside the page component to access the resolved value.
| Hook | What 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.
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.
[...slug], the value is exposed under params['*'], not the name written in the folder.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.
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 type | SSG output | What renders on first visit |
|---|---|---|
| Static, no loader | Full pre-rendered HTML | Instant paint from HTML |
| Static, with loader | Shell page (empty #root) | Loading UI, then client render |
| Dynamic, with loader | Shell page (empty #root) | Loading UI, then client render |
staticParams. Those URLs are rendered even when the route is client-only.Handle form submissions and mutations.
Access loader data and navigation state.
Catch render errors and show a fallback UI.
Show a fallback while pages and layouts load.
Match URL segments with dynamic parameters and catch-alls.
How every route is pre-rendered to HTML at build time.
Fetch data before a route renders with loader exports and useLoaderData().
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().
| Export | Type | When it runs |
|---|---|---|
| loader | function (async or sync) | Before the route renders |
| action | function (async or sync) | On form submission or POST/PUT/DELETE |
| shouldRevalidate | function | Before a revalidation |
| handle | object | Static route metadata for useMatches() |
clientOnly and gets a shell page during static generation instead of server-rendered HTML.Export a function called loader from your page. It can be sync or async. Return any serializable value.
Call useLoaderData() inside the page component to access the resolved value.
| Hook | What 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.
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.
[...slug], the value is exposed under params['*'], not the name written in the folder.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.
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 type | SSG output | What renders on first visit |
|---|---|---|
| Static, no loader | Full pre-rendered HTML | Instant paint from HTML |
| Static, with loader | Shell page (empty #root) | Loading UI, then client render |
| Dynamic, with loader | Shell page (empty #root) | Loading UI, then client render |
staticParams. Those URLs are rendered even when the route is client-only.Handle form submissions and mutations.
Access loader data and navigation state.
Catch render errors and show a fallback UI.
Show a fallback while pages and layouts load.
Match URL segments with dynamic parameters and catch-alls.
How every route is pre-rendered to HTML at build time.