@module-federation/bridge-react provides bridge utility functions for React applications:
createBridgeComponent: Used for exporting application-level modules, suitable for producers to wrap modules exported as application types.createRemoteComponent: Used for loading application-level modules, suitable for consumers to load modules as application types.Note: After using @module-federation/bridge-react, you cannot set react-router-dom as shared, otherwise the build tool will throw an exception. This is because @module-federation/bridge-react implements route control by proxying react-router-dom to ensure that inter-application routing works correctly.
In the producer project, assuming we need to export the application as an application type module using
@module-federation/bridge-react, with App.tsx as the application entry point.
export-app.tsx, which will be the file exported as an application type module. We need to use createBridgeComponent to wrap the root component of the application.export-app.tsx as an application type moduleAt this point, we have completed the export of the application type module.
Why do application type modules need to be wrapped with createBridgeComponent? There are three main reasons:
createBridgeComponent will conform to the loading protocol of the application type consumer, making cross-framework rendering possible.basename. Components wrapped with createBridgeComponent will automatically inject basename, ensuring that the producer application works correctly under the consumer project.createBridgeComponent will be wrapped with ErrorBoundary to ensure that fallback logic is automatically entered when remote loading fails or rendering errors occur.Host
createRemoteComponent to load the application type moduleAt this point, we have completed loading the application type module.
:::info
The remote module exported by createRemoteComponent will automatically use the react-bridge loading protocol to load the module,
making cross-framework rendering of applications possible.
Additionally, createRemoteComponent will automatically handle module loading, module destruction, error handling, loading, routing, and other logic,
allowing developers to focus solely on how to use the remote component.
For remote modules exported through createRemoteComponent, you can use them like regular React components: passing className, style, props, ref, and other attributes will be automatically passed through to the remote component,
making the user experience almost equivalent to using local components.
bridgeInfo
type:
options
loader
() => Promise<Module>loadRemote('remote1/export-app'), import('remote1/export-app')export
stringloading
React.ReactNodefallback
ComponentType<{ error: any; }>ReturnType
(props: PropsInfo)=> React.JSX.Element