@@ -58,11 +58,7 @@ configureDTL({
5858 }
5959 } ,
6060 eventWrapper : cb => {
61- let result
62- act ( ( ) => {
63- result = cb ( )
64- } )
65- return result
61+ return act ( cb )
6662 } ,
6763} )
6864
@@ -89,13 +85,13 @@ function wrapUiIfNeeded(innerElement, wrapperComponent) {
8985 : innerElement
9086}
9187
92- function createConcurrentRoot (
88+ async function createConcurrentRoot (
9389 container ,
9490 { hydrate, onCaughtError, onRecoverableError, ui, wrapper : WrapperComponent } ,
9591) {
9692 let root
9793 if ( hydrate ) {
98- act ( ( ) => {
94+ await act ( ( ) => {
9995 root = ReactDOMClient . hydrateRoot (
10096 container ,
10197 strictModeIfNeeded ( wrapUiIfNeeded ( ui , WrapperComponent ) ) ,
@@ -120,45 +116,53 @@ function createConcurrentRoot(
120116 // Nothing to do since hydration happens when creating the root object.
121117 } ,
122118 render ( element ) {
123- root . render ( element )
119+ return act ( ( ) => {
120+ root . render ( element )
121+ } )
124122 } ,
125123 unmount ( ) {
126- root . unmount ( )
124+ return act ( ( ) => {
125+ root . unmount ( )
126+ } )
127127 } ,
128128 }
129129}
130130
131- function createLegacyRoot ( container ) {
131+ async function createLegacyRoot ( container ) {
132132 return {
133133 hydrate ( element ) {
134- ReactDOM . hydrate ( element , container )
134+ return act ( ( ) => {
135+ ReactDOM . hydrate ( element , container )
136+ } )
135137 } ,
136138 render ( element ) {
137- ReactDOM . render ( element , container )
139+ return act ( ( ) => {
140+ ReactDOM . render ( element , container )
141+ } )
138142 } ,
139143 unmount ( ) {
140- ReactDOM . unmountComponentAtNode ( container )
144+ return act ( ( ) => {
145+ ReactDOM . unmountComponentAtNode ( container )
146+ } )
141147 } ,
142148 }
143149}
144150
145- function renderRoot (
151+ async function renderRoot (
146152 ui ,
147153 { baseElement, container, hydrate, queries, root, wrapper : WrapperComponent } ,
148154) {
149- act ( ( ) => {
150- if ( hydrate ) {
151- root . hydrate (
152- strictModeIfNeeded ( wrapUiIfNeeded ( ui , WrapperComponent ) ) ,
153- container ,
154- )
155- } else {
156- root . render (
157- strictModeIfNeeded ( wrapUiIfNeeded ( ui , WrapperComponent ) ) ,
158- container ,
159- )
160- }
161- } )
155+ if ( hydrate ) {
156+ await root . hydrate (
157+ strictModeIfNeeded ( wrapUiIfNeeded ( ui , WrapperComponent ) ) ,
158+ container ,
159+ )
160+ } else {
161+ await root . render (
162+ strictModeIfNeeded ( wrapUiIfNeeded ( ui , WrapperComponent ) ) ,
163+ container ,
164+ )
165+ }
162166
163167 return {
164168 container,
@@ -170,12 +174,10 @@ function renderRoot(
170174 : // eslint-disable-next-line no-console,
171175 console . log ( prettyDOM ( el , maxLength , options ) ) ,
172176 unmount : ( ) => {
173- act ( ( ) => {
174- root . unmount ( )
175- } )
177+ return root . unmount ( )
176178 } ,
177- rerender : rerenderUi => {
178- renderRoot ( rerenderUi , {
179+ rerender : async rerenderUi => {
180+ await renderRoot ( rerenderUi , {
179181 container,
180182 baseElement,
181183 root,
@@ -200,7 +202,7 @@ function renderRoot(
200202 }
201203}
202204
203- function render (
205+ async function render (
204206 ui ,
205207 {
206208 container,
@@ -242,7 +244,7 @@ function render(
242244 // eslint-disable-next-line no-negated-condition -- we want to map the evolution of this over time. The root is created first. Only later is it re-used so we don't want to read the case that happens later first.
243245 if ( ! mountedContainers . has ( container ) ) {
244246 const createRootImpl = legacyRoot ? createLegacyRoot : createConcurrentRoot
245- root = createRootImpl ( container , {
247+ root = await createRootImpl ( container , {
246248 hydrate,
247249 onCaughtError,
248250 onRecoverableError,
@@ -276,20 +278,22 @@ function render(
276278 } )
277279}
278280
279- function cleanup ( ) {
280- mountedRootEntries . forEach ( ( { root, container} ) => {
281- act ( ( ) => {
282- root . unmount ( )
283- } )
284- if ( container . parentNode === document . body ) {
285- document . body . removeChild ( container )
286- }
287- } )
281+ async function cleanup ( ) {
282+ await Promise . all (
283+ mountedRootEntries . map ( async ( { root, container} ) => {
284+ await act ( ( ) => {
285+ root . unmount ( )
286+ } )
287+ if ( container . parentNode === document . body ) {
288+ document . body . removeChild ( container )
289+ }
290+ } ) ,
291+ )
288292 mountedRootEntries . length = 0
289293 mountedContainers . clear ( )
290294}
291295
292- function renderHook ( renderCallback , options = { } ) {
296+ async function renderHook ( renderCallback , options = { } ) {
293297 const { initialProps, ...renderOptions } = options
294298
295299 if ( renderOptions . legacyRoot && typeof ReactDOM . render !== 'function' ) {
@@ -314,7 +318,7 @@ function renderHook(renderCallback, options = {}) {
314318 return null
315319 }
316320
317- const { rerender : baseRerender , unmount} = render (
321+ const { rerender : baseRerender , unmount} = await render (
318322 < TestComponent renderCallbackProps = { initialProps } /> ,
319323 renderOptions ,
320324 )
0 commit comments