@@ -36,6 +36,8 @@ import {
36
36
} from './ReactFiberScheduler' ;
37
37
38
38
import invariant from 'shared/invariant' ;
39
+ import warning from 'shared/warning' ;
40
+ import getComponentName from 'shared/getComponentName' ;
39
41
import areHookInputsEqual from 'shared/areHookInputsEqual' ;
40
42
41
43
type Update < S , A > = {
@@ -105,8 +107,6 @@ let componentUpdateQueue: FunctionComponentUpdateQueue | null = null;
105
107
// map of queue -> render-phase updates, which are discarded once the component
106
108
// completes without re-rendering.
107
109
108
- // Whether the work-in-progress hook is a re-rendered hook
109
- let isReRender : boolean = false ;
110
110
// Whether an update was scheduled during the currently executing render pass.
111
111
let didScheduleRenderPhaseUpdate : boolean = false ;
112
112
// Lazily created map of render-phase updates
@@ -148,11 +148,12 @@ export function renderWithHooks(
148
148
// remainingExpirationTime = NoWork;
149
149
// componentUpdateQueue = null;
150
150
151
- // isReRender = false;
152
151
// didScheduleRenderPhaseUpdate = false;
153
152
// renderPhaseUpdates = null;
154
153
// numberOfReRenders = -1;
155
154
155
+ const renderedWork : Fiber = ( currentlyRenderingFiber : any ) ;
156
+
156
157
let children ;
157
158
do {
158
159
didScheduleRenderPhaseUpdate = false ;
@@ -164,13 +165,26 @@ export function renderWithHooks(
164
165
componentUpdateQueue = null ;
165
166
166
167
children = Component ( props , refOrContext ) ;
168
+
169
+ if ( __DEV__ ) {
170
+ if (
171
+ current !== null &&
172
+ workInProgressHook !== null &&
173
+ currentHook === null
174
+ ) {
175
+ warning (
176
+ false ,
177
+ '%s: Rendered more hooks than during the previous render. This is ' +
178
+ 'not currently supported and may lead to unexpected behavior.' ,
179
+ getComponentName ( Component ) ,
180
+ ) ;
181
+ }
182
+ }
167
183
} while ( didScheduleRenderPhaseUpdate ) ;
168
184
169
185
renderPhaseUpdates = null ;
170
186
numberOfReRenders = - 1 ;
171
187
172
- const renderedWork : Fiber = ( currentlyRenderingFiber : any ) ;
173
-
174
188
if (
175
189
current !== null &&
176
190
( renderedWork . effectTag & PerformedWork ) === NoEffect
@@ -201,9 +215,6 @@ export function renderWithHooks(
201
215
remainingExpirationTime = NoWork ;
202
216
componentUpdateQueue = null ;
203
217
204
- // Always set during createWorkInProgress
205
- // isReRender = false;
206
-
207
218
// These were reset above
208
219
// didScheduleRenderPhaseUpdate = false;
209
220
// renderPhaseUpdates = null;
@@ -237,9 +248,6 @@ export function resetHooks(): void {
237
248
remainingExpirationTime = NoWork ;
238
249
componentUpdateQueue = null ;
239
250
240
- // Always set during createWorkInProgress
241
- // isReRender = false;
242
-
243
251
didScheduleRenderPhaseUpdate = false ;
244
252
renderPhaseUpdates = null ;
245
253
numberOfReRenders = - 1 ;
@@ -273,7 +281,6 @@ function createWorkInProgressHook(): Hook {
273
281
if ( workInProgressHook === null ) {
274
282
// This is the first hook in the list
275
283
if ( firstWorkInProgressHook === null ) {
276
- isReRender = false ;
277
284
currentHook = firstCurrentHook ;
278
285
if ( currentHook === null ) {
279
286
// This is a newly mounted hook
@@ -285,13 +292,11 @@ function createWorkInProgressHook(): Hook {
285
292
firstWorkInProgressHook = workInProgressHook ;
286
293
} else {
287
294
// There's already a work-in-progress. Reuse it.
288
- isReRender = true ;
289
295
currentHook = firstCurrentHook ;
290
296
workInProgressHook = firstWorkInProgressHook ;
291
297
}
292
298
} else {
293
299
if ( workInProgressHook . next === null ) {
294
- isReRender = false ;
295
300
let hook ;
296
301
if ( currentHook === null ) {
297
302
// This is a newly mounted hook
@@ -310,7 +315,6 @@ function createWorkInProgressHook(): Hook {
310
315
workInProgressHook = workInProgressHook . next = hook ;
311
316
} else {
312
317
// There's already a work-in-progress. Reuse it.
313
- isReRender = true ;
314
318
workInProgressHook = workInProgressHook . next ;
315
319
currentHook = currentHook !== null ? currentHook . next : null ;
316
320
}
@@ -358,7 +362,7 @@ export function useReducer<S, A>(
358
362
let queue : UpdateQueue < S , A > | null = ( workInProgressHook . queue : any ) ;
359
363
if ( queue !== null ) {
360
364
// Already have a queue, so this is an update.
361
- if ( isReRender ) {
365
+ if ( numberOfReRenders > 0 ) {
362
366
// This is a re-render. Apply the new render phase updates to the previous
363
367
// work-in-progress hook.
364
368
const dispatch : Dispatch < A > = ( queue . dispatch : any ) ;
0 commit comments