@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 typescreateRemoteComponent
: Used for loading application-level modules, suitable for consumers to load modules as application typesNote: 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.rsbuild.config.ts
configuration file, we need to export 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
rsbuild.config.ts
configuration, we need to register remote modules, which is no different from other Module Federation configurations.createRemoteComponent
to load the application type moduleAt this point, we have completed loading the application type module.
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.
createBridgeComponent
is used to wrap React components into remotely loadable modules.
createRemoteComponent
is used to load remote React components.