@@ -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, 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 ) ) ,
@@ -116,45 +112,53 @@ function createConcurrentRoot(
116112 // Nothing to do since hydration happens when creating the root object.
117113 } ,
118114 render ( element ) {
119- root . render ( element )
115+ return act ( ( ) => {
116+ root . render ( element )
117+ } )
120118 } ,
121119 unmount ( ) {
122- root . unmount ( )
120+ return act ( ( ) => {
121+ root . unmount ( )
122+ } )
123123 } ,
124124 }
125125}
126126
127- function createLegacyRoot ( container ) {
127+ async function createLegacyRoot ( container ) {
128128 return {
129129 hydrate ( element ) {
130- ReactDOM . hydrate ( element , container )
130+ return act ( ( ) => {
131+ ReactDOM . hydrate ( element , container )
132+ } )
131133 } ,
132134 render ( element ) {
133- ReactDOM . render ( element , container )
135+ return act ( ( ) => {
136+ ReactDOM . render ( element , container )
137+ } )
134138 } ,
135139 unmount ( ) {
136- ReactDOM . unmountComponentAtNode ( container )
140+ return act ( ( ) => {
141+ ReactDOM . unmountComponentAtNode ( container )
142+ } )
137143 } ,
138144 }
139145}
140146
141- function renderRoot (
147+ async function renderRoot (
142148 ui ,
143149 { baseElement, container, hydrate, queries, root, wrapper : WrapperComponent } ,
144150) {
145- act ( ( ) => {
146- if ( hydrate ) {
147- root . hydrate (
148- strictModeIfNeeded ( wrapUiIfNeeded ( ui , WrapperComponent ) ) ,
149- container ,
150- )
151- } else {
152- root . render (
153- strictModeIfNeeded ( wrapUiIfNeeded ( ui , WrapperComponent ) ) ,
154- container ,
155- )
156- }
157- } )
151+ if ( hydrate ) {
152+ await root . hydrate (
153+ strictModeIfNeeded ( wrapUiIfNeeded ( ui , WrapperComponent ) ) ,
154+ container ,
155+ )
156+ } else {
157+ await root . render (
158+ strictModeIfNeeded ( wrapUiIfNeeded ( ui , WrapperComponent ) ) ,
159+ container ,
160+ )
161+ }
158162
159163 return {
160164 container,
@@ -166,12 +170,10 @@ function renderRoot(
166170 : // eslint-disable-next-line no-console,
167171 console . log ( prettyDOM ( el , maxLength , options ) ) ,
168172 unmount : ( ) => {
169- act ( ( ) => {
170- root . unmount ( )
171- } )
173+ return root . unmount ( )
172174 } ,
173- rerender : rerenderUi => {
174- renderRoot ( rerenderUi , {
175+ rerender : async rerenderUi => {
176+ await renderRoot ( rerenderUi , {
175177 container,
176178 baseElement,
177179 root,
@@ -196,7 +198,7 @@ function renderRoot(
196198 }
197199}
198200
199- function render (
201+ async function render (
200202 ui ,
201203 {
202204 container,
@@ -230,7 +232,7 @@ function render(
230232 // 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.
231233 if ( ! mountedContainers . has ( container ) ) {
232234 const createRootImpl = legacyRoot ? createLegacyRoot : createConcurrentRoot
233- root = createRootImpl ( container , { hydrate, ui, wrapper} )
235+ root = await createRootImpl ( container , { hydrate, ui, wrapper} )
234236
235237 mountedRootEntries . push ( { container, root} )
236238 // we'll add it to the mounted containers regardless of whether it's actually
@@ -258,20 +260,22 @@ function render(
258260 } )
259261}
260262
261- function cleanup ( ) {
262- mountedRootEntries . forEach ( ( { root, container} ) => {
263- act ( ( ) => {
264- root . unmount ( )
265- } )
266- if ( container . parentNode === document . body ) {
267- document . body . removeChild ( container )
268- }
269- } )
263+ async function cleanup ( ) {
264+ await Promise . all (
265+ mountedRootEntries . map ( async ( { root, container} ) => {
266+ await act ( ( ) => {
267+ root . unmount ( )
268+ } )
269+ if ( container . parentNode === document . body ) {
270+ document . body . removeChild ( container )
271+ }
272+ } ) ,
273+ )
270274 mountedRootEntries . length = 0
271275 mountedContainers . clear ( )
272276}
273277
274- function renderHook ( renderCallback , options = { } ) {
278+ async function renderHook ( renderCallback , options = { } ) {
275279 const { initialProps, ...renderOptions } = options
276280
277281 if ( renderOptions . legacyRoot && typeof ReactDOM . render !== 'function' ) {
@@ -296,7 +300,7 @@ function renderHook(renderCallback, options = {}) {
296300 return null
297301 }
298302
299- const { rerender : baseRerender , unmount} = render (
303+ const { rerender : baseRerender , unmount} = await render (
300304 < TestComponent renderCallbackProps = { initialProps } /> ,
301305 renderOptions ,
302306 )
0 commit comments