@@ -16,11 +16,55 @@ export interface Root {
16
16
unmount ( ) : void ;
17
17
}
18
18
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
+
19
28
export function createReact16Or17Root (
20
29
container : Element | DocumentFragment ,
21
30
) : Root {
22
31
return {
23
32
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
+
24
68
// @ts -ignore - React 17's render method is deprecated but still functional
25
69
ReactDOM . render ( children , container ) ;
26
70
} ,
@@ -34,8 +78,8 @@ export function createBridgeComponent<T = any>(
34
78
bridgeInfo : Omit < ProviderFnParams < T > , 'createRoot' > ,
35
79
) {
36
80
const fullBridgeInfo = {
37
- ...bridgeInfo ,
38
81
createRoot : createReact16Or17Root ,
82
+ ...bridgeInfo ,
39
83
} as unknown as ProviderFnParams < T > ;
40
84
41
85
return createBaseBridgeComponent ( fullBridgeInfo ) ;
0 commit comments