@@ -16,11 +16,55 @@ export interface Root {
1616 unmount ( ) : void ;
1717}
1818
19+ /**
20+ * Default createRoot function that automatically detects React version and uses the appropriate API(only support React 16/17, 18)
21+ *
22+ * Note: Users can also directly import version-specific bridge components:
23+ * - import { createBridgeComponent } from '@module-federation/bridge-react'
24+ * - import { createBridgeComponent } from '@module-federation/bridge-react/v18'
25+ * - import { createBridgeComponent } from '@module-federation/bridge-react/v19'
26+ */
27+
1928export function createReact16Or17Root (
2029 container : Element | DocumentFragment ,
2130) : Root {
2231 return {
2332 render ( children : React . ReactNode ) {
33+ /**
34+ * Detect React version
35+ */
36+ const reactVersion = ReactDOM . version || '' ;
37+ const isReact18 = reactVersion . startsWith ( '18' ) ;
38+ const isReact19 = reactVersion . startsWith ( '19' ) ;
39+
40+ /**
41+ * Throw error for React 19
42+ *
43+ * Note: Due to Module Federation sharing mechanism, the actual version detected here
44+ * might be 18 or 19, even if the application itself uses React 16/17.
45+ * This happens because in MF environments, different remote modules may share different React versions.
46+ * The console may throw warnings about version and API mismatches. If you need to resolve these issues,
47+ * consider disabling the shared configuration for React.
48+ */
49+ if ( isReact19 ) {
50+ throw new Error (
51+ `React 19 detected in legacy mode. This is not supported. ` +
52+ `Please use the version-specific import: ` +
53+ `import { createBridgeComponent } from '@module-federation/bridge-react/v19'` ,
54+ ) ;
55+ }
56+
57+ /**
58+ * Provide warning for React 18
59+ */
60+ if ( isReact18 ) {
61+ console . warn (
62+ `[Bridge-React] React 18 detected in legacy mode. ` +
63+ `For better compatibility, please use the version-specific import: ` +
64+ `import { createBridgeComponent } from '@module-federation/bridge-react/v18'` ,
65+ ) ;
66+ }
67+
2468 // @ts -ignore - React 17's render method is deprecated but still functional
2569 ReactDOM . render ( children , container ) ;
2670 } ,
@@ -34,8 +78,8 @@ export function createBridgeComponent<T = any>(
3478 bridgeInfo : Omit < ProviderFnParams < T > , 'createRoot' > ,
3579) {
3680 const fullBridgeInfo = {
37- ...bridgeInfo ,
3881 createRoot : createReact16Or17Root ,
82+ ...bridgeInfo ,
3983 } as unknown as ProviderFnParams < T > ;
4084
4185 return createBaseBridgeComponent ( fullBridgeInfo ) ;
0 commit comments