This chapter will introduce how to use createRemoteAppComponent
to load and integrate remote React applications in host applications.
createRemoteAppComponent
is the core API of React Bridge for loading remote React applications in host applications. It has the following features:
Add remote application configuration to the host application's configuration file:
The following example uses Rsbuild configuration. Please adjust according to your build tool:
@module-federation/rsbuild-plugin
@module-federation/enhanced/rspack
@module-federation/enhanced/webpack
@module-federation/vite
First, create loading state and error handling components:
Use createRemoteAppComponent
to create remote components:
Configure routing in the main application:
basename
: Set the base path for the remote applicationmemoryRoute
: Memory route configuration, used to control sub-application routing as memoryRouterstyle
: React.CSSProperties - Set component stylesclassName
: string - Set component class nameref
: React.Ref<HTMLDivElement> - Forward reference to internal container element for DOM manipulationprops
: Property object passed to remote componentuserId={'123'}
配置参数接口:
返回组件的属性接口:
() => Promise<T>
React.ReactNode
React.ComponentType<{ error: Error }>
error
属性类型: E extends keyof T
(泛型约束,通常是 string
)
必需: 否
默认值: 'default'
作用: 指定要使用的远程模块导出名称
示例:
假设远程模块有以下导出:
在宿主应用中可以这样使用: