Route-level code splitting
A route component should be downloaded only when its route is needed.
TanStack · Function
Creates a one-off, code-split TanStack Router route component. The returned lazy component can be preloaded, and an optional export name supports named exports.
Pass an importer that resolves to a module containing the route component. The function returns a React.lazy component and adds a preload method, while an optional export name selects a named export instead of the module's default export.
lazyRouteComponent(importer, exportName?)| Name | Type | Description |
|---|---|---|
importer | () => Promise<T> | Function returning a promise that resolves to an object containing the component. |
exportName | string | Optional exported component name; defaults to the default export. |
A React.lazy component that can be preloaded using component.preload().
import { createRoute, lazyRouteComponent } from '@tanstack/react-router'
const postsRoute = createRoute({
getParentRoute: () => rootRoute,
path: '/posts/$postId',
component: lazyRouteComponent(() => import('./Post')),
})The route loads its default-exported component on demand, splitting the route component into a separate bundle.
A route component should be downloaded only when its route is needed.
Navigation or interaction can begin loading a route before rendering it.
The component is exported by name rather than as the module default.
It accepts an importer and an optional exportName. The importer must return a promise resolving to a module containing the component; exportName identifies which export to use and defaults to the default export.
Follow-up: How can the resulting component be loaded before rendering? Call its preload() method, which is exposed on the returned lazy component.
It creates a one-off code-split route component. The returned value is a React.lazy component that supports preloading through component.preload().
Follow-up: When should this helper generally be avoided? In file-based routing, use createLazyFileRoute instead.
Use the default form when the module's component is the default export. Pass the export's name as the second argument when the component is exported by name.
Follow-up: What must the first argument provide? A function returning a promise for an object that contains the component to load.
createLazyFileRoutecreateRouteReact.lazy