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/viteFirst, 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.ReactNodeReact.ComponentType<{ error: Error }>error 属性类型: E extends keyof T (泛型约束,通常是 string)
必需: 否
默认值: 'default'
作用: 指定要使用的远程模块导出名称
示例:
假设远程模块有以下导出:
在宿主应用中可以这样使用:
By default, @module-federation/bridge-react includes react-router-dom in your bundle to provide the following out-of-the-box capabilities:
However, if your project meets any of these conditions:
We recommend disabling the enableBridgeRouter configuration to turn off this capability, which will:
You can control whether to include router support through the bridge.enableBridgeRouter configuration:
enableBridgeRouter: false: Automatically aliases to /base entry point (no react-router-dom code)enableBridgeRouter: true or undefined: Includes router support (default behavior)
:::Disable router (enableBridgeRouter: false) when:
Keep router enabled (default) when:
:::info How It Works
When enableBridgeRouter: false, the Module Federation plugin automatically sets up a webpack alias:
This means your imports automatically resolve to the router-free version without changing any code!