# Dynamic imports
Utility functions for lazy loading React components.
## Dynamic imports
The `dynamic` utility from `@redocly/theme` creates React components that load their dependencies on demand.
It supports both server-side rendering and client-only rendering modes, with customizable loading states.
## Overview
The `dynamic` function wraps React.lazy() with additional features for Redocly applications, automatically handling various export patterns and providing flexible SSR configuration options.
It creates wrapper components that handle dynamic imports with configurable loading states.
## Syntax and usage
To use the `dynamic` utility, pass a factory function that returns a promise resolving to a React component:
```tsx
import { dynamic } from '@redocly/theme';
const DynamicComponent = dynamic(() => import('../components/MyComponent'), {
loading: () =>
Loading...
,
});
export default function Home() {
return ;
}
```
## API reference
### `dynamic(factory, options?)`
Creates a dynamically loaded React component.
| Parameter | Type | Description |
| --- | --- | --- |
| factory | `() => Promise>` | **REQUIRED.** A function that returns a promise resolving to a React component or module containing a component. |
| options | `DynamicOptions` | Optional configuration for the dynamic component. |
### `DynamicOptions`
| Option | Type | Description |
| --- | --- | --- |
| ssr | `boolean` | Enable server-side rendering. Default: `false` |
| loading | `React.ComponentType>` | Custom loading component to display while the dynamic component loads. Default: `null` |
## Examples
### Basic dynamic import
```tsx
import { dynamic } from '@redocly/theme';
const MyComponent = dynamic(() => import('./MyComponent'));
function App() {
return ;
}
```
### With loading component
```tsx
import { dynamic } from '@redocly/theme';
const DynamicComponent = dynamic(() => import('../components/MyComponent'), {
loading: () => Loading...
,
});
export default function Home() {
return ;
}
```
### With named exports
To dynamically import a named export, return it from the Promise returned by import():
```tsx
// components/MyComponent.js
export function MyComponent() {
return Hello!
;
}
// pages/index.js
import { dynamic } from '@redocly/theme';
const DynamicComponent = dynamic(() =>
import('../components/MyComponent').then((mod) => mod.MyComponent)
);
```
### With no SSR
To dynamically load a component on the client side, use the `ssr` option to disable server-rendering.
This is useful for components that rely on browser APIs like `window`.
```tsx
import { dynamic } from '@redocly/theme';
const ClientOnlyComponent = dynamic(() => import('../components/ClientComponent'), {
ssr: false,
});
```
### With external libraries
This example shows how to dynamically load components that depend on external libraries:
```tsx
import { dynamic } from '@redocly/theme';
const ChartComponent = dynamic(() => import('./ChartComponent'), {
loading: () => Loading chart...
,
ssr: false, // Charts often need browser APIs
});
const MapComponent = dynamic(() => import('./MapComponent'), {
loading: () => Loading map...
,
ssr: false, // Maps need browser APIs
});
function App() {
return (
Dashboard
);
}
```
The components `ChartComponent` and `MapComponent` can internally use external libraries like Chart.js or Leaflet, which are loaded only when the components are rendered.
## Import patterns
The dynamic utility automatically handles various import patterns:
```tsx
// Default exports
const Component1 = dynamic(() => import('./Component1'));
const Component2 = dynamic(() => import('./Component2').then(mod => mod.default));
// Named exports
const NamedComponent = dynamic(() => import('./NamedComponent').then(mod => mod.NamedExport));
// Package imports
const PackageComponent = dynamic(() => import('some-package').then(mod => mod.default));
```
## Advanced examples
### Heavy component loading
```tsx
import { dynamic } from '@redocly/theme';
const HeavyComponent = dynamic(() => import('./HeavyComponent'), {
loading: () => Loading component...
,
ssr: false,
});
function App() {
return (
Application
);
}
```