@@ -27,6 +27,7 @@ import {
2727
2828import {
2929 createContainer ,
30+ createHydrationContainer ,
3031 findHostInstanceWithNoPortals ,
3132 updateContainer ,
3233 flushSync ,
@@ -109,34 +110,81 @@ function noopOnRecoverableError() {
109110
110111function legacyCreateRootFromDOMContainer (
111112 container : Container ,
112- forceHydrate : boolean ,
113+ initialChildren : ReactNodeList ,
114+ parentComponent : ?React$Component < any , any > ,
115+ callback : ?Function ,
116+ isHydrationContainer : boolean ,
113117) : FiberRoot {
114- // First clear any existing content.
115- if ( ! forceHydrate ) {
118+ if ( isHydrationContainer ) {
119+ if ( typeof callback === 'function' ) {
120+ const originalCallback = callback ;
121+ callback = function ( ) {
122+ const instance = getPublicRootInstance ( root ) ;
123+ originalCallback . call ( instance ) ;
124+ } ;
125+ }
126+
127+ const root = createHydrationContainer (
128+ initialChildren ,
129+ callback ,
130+ container ,
131+ LegacyRoot ,
132+ null , // hydrationCallbacks
133+ false , // isStrictMode
134+ false , // concurrentUpdatesByDefaultOverride,
135+ '' , // identifierPrefix
136+ noopOnRecoverableError ,
137+ // TODO(luna) Support hydration later
138+ null ,
139+ ) ;
140+ container . _reactRootContainer = root ;
141+ markContainerAsRoot ( root . current , container ) ;
142+
143+ const rootContainerElement =
144+ container . nodeType === COMMENT_NODE ? container . parentNode : container ;
145+ listenToAllSupportedEvents ( rootContainerElement ) ;
146+
147+ flushSync ( ) ;
148+ return root ;
149+ } else {
150+ // First clear any existing content.
116151 let rootSibling ;
117152 while ( ( rootSibling = container . lastChild ) ) {
118153 container . removeChild ( rootSibling ) ;
119154 }
120- }
121155
122- const root = createContainer (
123- container ,
124- LegacyRoot ,
125- forceHydrate ,
126- null , // hydrationCallbacks
127- false , // isStrictMode
128- false , // concurrentUpdatesByDefaultOverride,
129- '' , // identifierPrefix
130- noopOnRecoverableError , // onRecoverableError
131- null , // transitionCallbacks
132- ) ;
133- markContainerAsRoot ( root . current , container ) ;
156+ if ( typeof callback === 'function' ) {
157+ const originalCallback = callback ;
158+ callback = function ( ) {
159+ const instance = getPublicRootInstance ( root ) ;
160+ originalCallback . call ( instance ) ;
161+ } ;
162+ }
163+
164+ const root = createContainer (
165+ container ,
166+ LegacyRoot ,
167+ null , // hydrationCallbacks
168+ false , // isStrictMode
169+ false , // concurrentUpdatesByDefaultOverride,
170+ '' , // identifierPrefix
171+ noopOnRecoverableError , // onRecoverableError
172+ null , // transitionCallbacks
173+ ) ;
174+ container . _reactRootContainer = root ;
175+ markContainerAsRoot ( root . current , container ) ;
176+
177+ const rootContainerElement =
178+ container . nodeType === COMMENT_NODE ? container . parentNode : container ;
179+ listenToAllSupportedEvents ( rootContainerElement ) ;
134180
135- const rootContainerElement =
136- container . nodeType === COMMENT_NODE ? container . parentNode : container ;
137- listenToAllSupportedEvents ( rootContainerElement ) ;
181+ // Initial mount should not be batched.
182+ flushSync ( ( ) => {
183+ updateContainer ( initialChildren , root , parentComponent , callback ) ;
184+ } ) ;
138185
139- return root ;
186+ return root ;
187+ }
140188}
141189
142190function warnOnInvalidCallback ( callback : mixed , callerName : string ) : void {
@@ -164,39 +212,30 @@ function legacyRenderSubtreeIntoContainer(
164212 warnOnInvalidCallback ( callback === undefined ? null : callback , 'render' ) ;
165213 }
166214
167- let root = container . _reactRootContainer ;
168- let fiberRoot : FiberRoot ;
169- if ( ! root ) {
215+ const maybeRoot = container . _reactRootContainer ;
216+ let root : FiberRoot ;
217+ if ( ! maybeRoot ) {
170218 // Initial mount
171- root = container . _reactRootContainer = legacyCreateRootFromDOMContainer (
219+ root = legacyCreateRootFromDOMContainer (
172220 container ,
221+ children ,
222+ parentComponent ,
223+ callback ,
173224 forceHydrate ,
174225 ) ;
175- fiberRoot = root ;
176- if ( typeof callback === 'function' ) {
177- const originalCallback = callback ;
178- callback = function ( ) {
179- const instance = getPublicRootInstance ( fiberRoot ) ;
180- originalCallback . call ( instance ) ;
181- } ;
182- }
183- // Initial mount should not be batched.
184- flushSync ( ( ) => {
185- updateContainer ( children , fiberRoot , parentComponent , callback ) ;
186- } ) ;
187226 } else {
188- fiberRoot = root ;
227+ root = maybeRoot ;
189228 if ( typeof callback === 'function' ) {
190229 const originalCallback = callback ;
191230 callback = function ( ) {
192- const instance = getPublicRootInstance ( fiberRoot ) ;
231+ const instance = getPublicRootInstance ( root ) ;
193232 originalCallback . call ( instance ) ;
194233 } ;
195234 }
196235 // Update
197- updateContainer ( children , fiberRoot , parentComponent , callback ) ;
236+ updateContainer ( children , root , parentComponent , callback ) ;
198237 }
199- return getPublicRootInstance ( fiberRoot ) ;
238+ return getPublicRootInstance ( root ) ;
200239}
201240
202241export function findDOMNode (
0 commit comments