@@ -57,11 +57,7 @@ configureDTL({
5757 }
5858 } ,
5959 eventWrapper : cb => {
60- let result
61- act ( ( ) => {
62- result = cb ( )
63- } )
64- return result
60+ return act ( cb )
6561 } ,
6662} )
6763
@@ -76,13 +72,13 @@ const mountedContainers = new Set()
7672 */
7773const mountedRootEntries = [ ]
7874
79- function createConcurrentRoot (
75+ async function createConcurrentRoot (
8076 container ,
8177 { hydrate, ui, wrapper : WrapperComponent } ,
8278) {
8379 let root
8480 if ( hydrate ) {
85- act ( ( ) => {
81+ await act ( ( ) => {
8682 root = ReactDOMClient . hydrateRoot (
8783 container ,
8884 WrapperComponent ? React . createElement ( WrapperComponent , null , ui ) : ui ,
@@ -103,29 +99,39 @@ function createConcurrentRoot(
10399 // Nothing to do since hydration happens when creating the root object.
104100 } ,
105101 render ( element ) {
106- root . render ( element )
102+ return act ( ( ) => {
103+ root . render ( element )
104+ } )
107105 } ,
108106 unmount ( ) {
109- root . unmount ( )
107+ return act ( ( ) => {
108+ root . unmount ( )
109+ } )
110110 } ,
111111 }
112112}
113113
114- function createLegacyRoot ( container ) {
114+ async function createLegacyRoot ( container ) {
115115 return {
116116 hydrate ( element ) {
117- ReactDOM . hydrate ( element , container )
117+ return act ( ( ) => {
118+ ReactDOM . hydrate ( element , container )
119+ } )
118120 } ,
119121 render ( element ) {
120- ReactDOM . render ( element , container )
122+ return act ( ( ) => {
123+ ReactDOM . render ( element , container )
124+ } )
121125 } ,
122126 unmount ( ) {
123- ReactDOM . unmountComponentAtNode ( container )
127+ return act ( ( ) => {
128+ ReactDOM . unmountComponentAtNode ( container )
129+ } )
124130 } ,
125131 }
126132}
127133
128- function renderRoot (
134+ async function renderRoot (
129135 ui ,
130136 { baseElement, container, hydrate, queries, root, wrapper : WrapperComponent } ,
131137) {
@@ -134,13 +140,11 @@ function renderRoot(
134140 ? React . createElement ( WrapperComponent , null , innerElement )
135141 : innerElement
136142
137- act ( ( ) => {
138- if ( hydrate ) {
139- root . hydrate ( wrapUiIfNeeded ( ui ) , container )
140- } else {
141- root . render ( wrapUiIfNeeded ( ui ) , container )
142- }
143- } )
143+ if ( hydrate ) {
144+ await root . hydrate ( wrapUiIfNeeded ( ui ) , container )
145+ } else {
146+ await root . render ( wrapUiIfNeeded ( ui ) , container )
147+ }
144148
145149 return {
146150 container,
@@ -152,12 +156,10 @@ function renderRoot(
152156 : // eslint-disable-next-line no-console,
153157 console . log ( prettyDOM ( el , maxLength , options ) ) ,
154158 unmount : ( ) => {
155- act ( ( ) => {
156- root . unmount ( )
157- } )
159+ return root . unmount ( )
158160 } ,
159- rerender : rerenderUi => {
160- renderRoot ( wrapUiIfNeeded ( rerenderUi ) , {
161+ rerender : async rerenderUi => {
162+ await renderRoot ( wrapUiIfNeeded ( rerenderUi ) , {
161163 container,
162164 baseElement,
163165 root,
@@ -181,7 +183,7 @@ function renderRoot(
181183 }
182184}
183185
184- function render (
186+ async function render (
185187 ui ,
186188 {
187189 container,
@@ -205,7 +207,7 @@ function render(
205207 // 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.
206208 if ( ! mountedContainers . has ( container ) ) {
207209 const createRootImpl = legacyRoot ? createLegacyRoot : createConcurrentRoot
208- root = createRootImpl ( container , { hydrate, ui, wrapper} )
210+ root = await createRootImpl ( container , { hydrate, ui, wrapper} )
209211
210212 mountedRootEntries . push ( { container, root} )
211213 // we'll add it to the mounted containers regardless of whether it's actually
@@ -233,20 +235,22 @@ function render(
233235 } )
234236}
235237
236- function cleanup ( ) {
237- mountedRootEntries . forEach ( ( { root, container} ) => {
238- act ( ( ) => {
239- root . unmount ( )
240- } )
241- if ( container . parentNode === document . body ) {
242- document . body . removeChild ( container )
243- }
244- } )
238+ async function cleanup ( ) {
239+ await Promise . all (
240+ mountedRootEntries . map ( async ( { root, container} ) => {
241+ await act ( ( ) => {
242+ root . unmount ( )
243+ } )
244+ if ( container . parentNode === document . body ) {
245+ document . body . removeChild ( container )
246+ }
247+ } ) ,
248+ )
245249 mountedRootEntries . length = 0
246250 mountedContainers . clear ( )
247251}
248252
249- function renderHook ( renderCallback , options = { } ) {
253+ async function renderHook ( renderCallback , options = { } ) {
250254 const { initialProps, ...renderOptions } = options
251255 const result = React . createRef ( )
252256
@@ -260,7 +264,7 @@ function renderHook(renderCallback, options = {}) {
260264 return null
261265 }
262266
263- const { rerender : baseRerender , unmount} = render (
267+ const { rerender : baseRerender , unmount} = await render (
264268 < TestComponent renderCallbackProps = { initialProps } /> ,
265269 renderOptions ,
266270 )
0 commit comments