@@ -57,11 +57,7 @@ configureDTL({
57
57
}
58
58
} ,
59
59
eventWrapper : cb => {
60
- let result
61
- act ( ( ) => {
62
- result = cb ( )
63
- } )
64
- return result
60
+ return act ( cb )
65
61
} ,
66
62
} )
67
63
@@ -76,13 +72,13 @@ const mountedContainers = new Set()
76
72
*/
77
73
const mountedRootEntries = [ ]
78
74
79
- function createConcurrentRoot (
75
+ async function createConcurrentRoot (
80
76
container ,
81
77
{ hydrate, ui, wrapper : WrapperComponent } ,
82
78
) {
83
79
let root
84
80
if ( hydrate ) {
85
- act ( ( ) => {
81
+ await act ( ( ) => {
86
82
root = ReactDOMClient . hydrateRoot (
87
83
container ,
88
84
WrapperComponent ? React . createElement ( WrapperComponent , null , ui ) : ui ,
@@ -103,29 +99,39 @@ function createConcurrentRoot(
103
99
// Nothing to do since hydration happens when creating the root object.
104
100
} ,
105
101
render ( element ) {
106
- root . render ( element )
102
+ return act ( ( ) => {
103
+ root . render ( element )
104
+ } )
107
105
} ,
108
106
unmount ( ) {
109
- root . unmount ( )
107
+ return act ( ( ) => {
108
+ root . unmount ( )
109
+ } )
110
110
} ,
111
111
}
112
112
}
113
113
114
- function createLegacyRoot ( container ) {
114
+ async function createLegacyRoot ( container ) {
115
115
return {
116
116
hydrate ( element ) {
117
- ReactDOM . hydrate ( element , container )
117
+ return act ( ( ) => {
118
+ ReactDOM . hydrate ( element , container )
119
+ } )
118
120
} ,
119
121
render ( element ) {
120
- ReactDOM . render ( element , container )
122
+ return act ( ( ) => {
123
+ ReactDOM . render ( element , container )
124
+ } )
121
125
} ,
122
126
unmount ( ) {
123
- ReactDOM . unmountComponentAtNode ( container )
127
+ return act ( ( ) => {
128
+ ReactDOM . unmountComponentAtNode ( container )
129
+ } )
124
130
} ,
125
131
}
126
132
}
127
133
128
- function renderRoot (
134
+ async function renderRoot (
129
135
ui ,
130
136
{ baseElement, container, hydrate, queries, root, wrapper : WrapperComponent } ,
131
137
) {
@@ -134,13 +140,11 @@ function renderRoot(
134
140
? React . createElement ( WrapperComponent , null , innerElement )
135
141
: innerElement
136
142
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
+ }
144
148
145
149
return {
146
150
container,
@@ -152,12 +156,10 @@ function renderRoot(
152
156
: // eslint-disable-next-line no-console,
153
157
console . log ( prettyDOM ( el , maxLength , options ) ) ,
154
158
unmount : ( ) => {
155
- act ( ( ) => {
156
- root . unmount ( )
157
- } )
159
+ return root . unmount ( )
158
160
} ,
159
- rerender : rerenderUi => {
160
- renderRoot ( wrapUiIfNeeded ( rerenderUi ) , {
161
+ rerender : async rerenderUi => {
162
+ await renderRoot ( wrapUiIfNeeded ( rerenderUi ) , {
161
163
container,
162
164
baseElement,
163
165
root,
@@ -181,7 +183,7 @@ function renderRoot(
181
183
}
182
184
}
183
185
184
- function render (
186
+ async function render (
185
187
ui ,
186
188
{
187
189
container,
@@ -205,7 +207,7 @@ function render(
205
207
// 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.
206
208
if ( ! mountedContainers . has ( container ) ) {
207
209
const createRootImpl = legacyRoot ? createLegacyRoot : createConcurrentRoot
208
- root = createRootImpl ( container , { hydrate, ui, wrapper} )
210
+ root = await createRootImpl ( container , { hydrate, ui, wrapper} )
209
211
210
212
mountedRootEntries . push ( { container, root} )
211
213
// we'll add it to the mounted containers regardless of whether it's actually
@@ -233,20 +235,22 @@ function render(
233
235
} )
234
236
}
235
237
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
+ )
245
249
mountedRootEntries . length = 0
246
250
mountedContainers . clear ( )
247
251
}
248
252
249
- function renderHook ( renderCallback , options = { } ) {
253
+ async function renderHook ( renderCallback , options = { } ) {
250
254
const { initialProps, ...renderOptions } = options
251
255
const result = React . createRef ( )
252
256
@@ -260,7 +264,7 @@ function renderHook(renderCallback, options = {}) {
260
264
return null
261
265
}
262
266
263
- const { rerender : baseRerender , unmount} = render (
267
+ const { rerender : baseRerender , unmount} = await render (
264
268
< TestComponent renderCallbackProps = { initialProps } /> ,
265
269
renderOptions ,
266
270
)
0 commit comments