Loading Components
This chapter will introduce how to use createLazyComponent to load and integrate remote React component modules in host applications.
What is createLazyComponent?
createLazyComponent is an API provided by React Bridge for loading individual remote components. Unlike createRemoteAppComponent, it is specifically designed for loading specific React components rather than complete applications.
createLazyComponent vs createRemoteAppComponent
Installation
Basic Usage
Step 1: Register lazyLoadComponentPlugin
Register the lazyLoadComponentPlugin plugin at runtime to enable createLazyComponent and prefetch APIs.
Step 2: Call createLazyComponent
After registering the lazyLoadComponentPlugin plugin, you can create lazy-loaded components using the instance.createLazyComponent method.
createLazyComponent API Reference
Function Signature
createLazyComponent
This API requires registering the lazyLoadComponentPlugin plugin first before it can be called.
Type declaration
In addition to loading components, this function also supports the following capabilities:
- In SSR mode, it will inject the corresponding producer's style tags/script resources, which can help avoid CSS flickering issues caused by streaming rendering and accelerate PID (First Paint Interactive Time).
- If the producer has a data fetching function, it will automatically call this function and inject the data.
loader
- Type:
() => Promise<T> - Required: Yes
- Default:
undefined
Function to load remote components, typically ()=>loadRemote(id) or ()=>import(id).
loading
- Type:
React.ReactNode - Required: Yes
- Default:
undefined
Sets the module loading state.
delayLoading
- Type:
number - Required: No
- Default:
undefined
Sets the delay time for displaying loading state in milliseconds. If the loading time is less than this time, the loading state will not be displayed.
fallback
- Type:
(({ error }: { error: ErrorInfo}) => React.ReactElement) - Required: Yes
- Default:
undefined
The error component rendered when component loading or rendering fails.
export
- Type:
string - Required: No
- Default:
'default'
If the remote component is a named export, you can specify the component name to export through this parameter. By default, it loads the default export.
dataFetchParams
- Type:
DataFetchParams - Required: No
- Default:
undefined
If the remote component has a data fetching function, this will be passed to the data fetching function when set.
noSSR
- Type:
boolean - Required: No
- Default:
false
When set to true, this component will not render in SSR scenarios.
injectScript
- Type:
boolean - Required: No
- Default:
false
In SSR environment, if set to true, the created component will inject the corresponding script resources.
For example, if remote/button has __federation_button.js, then in the HTML returned by SSR, the corresponding script will be injected before the component to accelerate interaction speed.
injectLink
- Type:
boolean - Required: No
- Default:
true
In SSR environment, if set to true, the created component will inject the corresponding style resource links.
For example, if remote/button has __federation_button.css, then in the HTML returned by SSR, the corresponding link will be injected before the component to avoid page flickering issues.
prefetch
This API requires registering the lazyLoadComponentPlugin plugin first before it can be called.
Type declaration
Preload component resource files and the component's data loader.
id
- Type:
string - Required: Yes
- Default:
undefined
The id of the component to preload.
preloadComponentResource
- Type:
boolean - Required: No
- Default:
false
Whether to preload the component's resource files.
dataFetchParams
- Type:
DataFetchParams - Required: No
- Default:
undefined
If the remote component has a data fetching function, this will be passed to the data fetching function when set.