@@ -14,15 +14,18 @@ import type {
14
14
CreateRootOptions ,
15
15
} from './ReactDOMRoot' ;
16
16
17
+ import { disableLegacyMode } from 'shared/ReactFeatureFlags' ;
17
18
import {
18
19
createRoot as createRootImpl ,
19
20
hydrateRoot as hydrateRootImpl ,
20
21
isValidContainer ,
21
22
} from './ReactDOMRoot' ;
22
23
import { createEventHandle } from 'react-dom-bindings/src/client/ReactDOMEventHandle' ;
23
- import { flushSync } from '../shared/ReactDOMFlushSync' ;
24
+ import { flushSync as flushSyncIsomorphic } from '../shared/ReactDOMFlushSync' ;
24
25
25
26
import {
27
+ flushSyncFromReconciler as flushSyncWithoutWarningIfAlreadyRendering ,
28
+ isAlreadyRendering ,
26
29
injectIntoDevTools ,
27
30
findHostInstance ,
28
31
} from 'react-reconciler/src/ReactFiberReconciler' ;
@@ -120,6 +123,29 @@ function hydrateRoot(
120
123
return hydrateRootImpl ( container , initialChildren, options) ;
121
124
}
122
125
126
+ // Overload the definition to the two valid signatures.
127
+ // Warning, this opts-out of checking the function body.
128
+ declare function flushSyncFromReconciler < R > ( fn : ( ) = > R ) : R ;
129
+ // eslint-disable-next-line no-redeclare
130
+ declare function flushSyncFromReconciler ( ) : void ;
131
+ // eslint-disable-next-line no-redeclare
132
+ function flushSyncFromReconciler < R > ( fn : ( ( ) => R ) | void ) : R | void {
133
+ if ( __DEV__ ) {
134
+ if ( isAlreadyRendering ( ) ) {
135
+ console . error (
136
+ 'flushSync was called from inside a lifecycle method. React cannot ' +
137
+ 'flush when React is already rendering. Consider moving this call to ' +
138
+ 'a scheduler task or micro task.' ,
139
+ ) ;
140
+ }
141
+ }
142
+ return flushSyncWithoutWarningIfAlreadyRendering ( fn ) ;
143
+ }
144
+
145
+ const flushSync : typeof flushSyncIsomorphic = disableLegacyMode
146
+ ? flushSyncIsomorphic
147
+ : flushSyncFromReconciler ;
148
+
123
149
function findDOMNode (
124
150
componentOrElement : React$Component < any , any > ,
125
151
) : null | Element | Text {
0 commit comments