Overview
Thelazy() function creates components that are loaded dynamically on demand, reducing initial bundle size and improving performance.
Signature
A function that returns a Promise resolving to a Component class. Typically used with dynamic
import().Configuration options for the lazy component.
Loading component or virtual DOM to show while loading. Defaults to a “Loading…” message.
Error component or virtual DOM to show on load failure. Receives
{ error } prop. Defaults to a red error message display.A factory function that creates the lazy component with the given props.
Usage
Basic Lazy Loading
Custom Loading State
With ES Module Default Export
Behavior
- Components start loading immediately when instantiated
- Loading state is shown while the module is being fetched
- Once loaded, the component is rendered with the provided props
- On error, the error state is displayed
- Handles both ES module default exports and direct Component classes
See Also
- Suspense - Alternative loading state pattern
- createDelayedComponent() - Testing utility for lazy loading
createDelayedComponent()
A utility function for testing lazy loading behavior by simulating network delay.Signature
The Component class to delay loading.
The delay in milliseconds before resolving the component.
A function that returns a Promise resolving to the Component after the specified delay.