@@ -36,6 +36,8 @@ import {
3636} from './ReactFiberScheduler' ;
3737
3838import invariant from 'shared/invariant' ;
39+ import warning from 'shared/warning' ;
40+ import getComponentName from 'shared/getComponentName' ;
3941import areHookInputsEqual from 'shared/areHookInputsEqual' ;
4042
4143type Update < S , A > = {
@@ -105,8 +107,6 @@ let componentUpdateQueue: FunctionComponentUpdateQueue | null = null;
105107// map of queue -> render-phase updates, which are discarded once the component
106108// completes without re-rendering.
107109
108- // Whether the work-in-progress hook is a re-rendered hook
109- let isReRender : boolean = false ;
110110// Whether an update was scheduled during the currently executing render pass.
111111let didScheduleRenderPhaseUpdate : boolean = false ;
112112// Lazily created map of render-phase updates
@@ -148,11 +148,12 @@ export function renderWithHooks(
148148 // remainingExpirationTime = NoWork;
149149 // componentUpdateQueue = null;
150150
151- // isReRender = false;
152151 // didScheduleRenderPhaseUpdate = false;
153152 // renderPhaseUpdates = null;
154153 // numberOfReRenders = -1;
155154
155+ const renderedWork : Fiber = ( currentlyRenderingFiber : any ) ;
156+
156157 let children ;
157158 do {
158159 didScheduleRenderPhaseUpdate = false ;
@@ -164,13 +165,26 @@ export function renderWithHooks(
164165 componentUpdateQueue = null ;
165166
166167 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+ }
167183 } while ( didScheduleRenderPhaseUpdate ) ;
168184
169185 renderPhaseUpdates = null ;
170186 numberOfReRenders = - 1 ;
171187
172- const renderedWork : Fiber = ( currentlyRenderingFiber : any ) ;
173-
174188 if (
175189 current !== null &&
176190 ( renderedWork . effectTag & PerformedWork ) === NoEffect
@@ -201,9 +215,6 @@ export function renderWithHooks(
201215 remainingExpirationTime = NoWork ;
202216 componentUpdateQueue = null ;
203217
204- // Always set during createWorkInProgress
205- // isReRender = false;
206-
207218 // These were reset above
208219 // didScheduleRenderPhaseUpdate = false;
209220 // renderPhaseUpdates = null;
@@ -237,9 +248,6 @@ export function resetHooks(): void {
237248 remainingExpirationTime = NoWork ;
238249 componentUpdateQueue = null ;
239250
240- // Always set during createWorkInProgress
241- // isReRender = false;
242-
243251 didScheduleRenderPhaseUpdate = false ;
244252 renderPhaseUpdates = null ;
245253 numberOfReRenders = - 1 ;
@@ -273,7 +281,6 @@ function createWorkInProgressHook(): Hook {
273281 if ( workInProgressHook === null ) {
274282 // This is the first hook in the list
275283 if ( firstWorkInProgressHook === null ) {
276- isReRender = false ;
277284 currentHook = firstCurrentHook ;
278285 if ( currentHook === null ) {
279286 // This is a newly mounted hook
@@ -285,13 +292,11 @@ function createWorkInProgressHook(): Hook {
285292 firstWorkInProgressHook = workInProgressHook ;
286293 } else {
287294 // There's already a work-in-progress. Reuse it.
288- isReRender = true ;
289295 currentHook = firstCurrentHook ;
290296 workInProgressHook = firstWorkInProgressHook ;
291297 }
292298 } else {
293299 if ( workInProgressHook . next === null ) {
294- isReRender = false ;
295300 let hook ;
296301 if ( currentHook === null ) {
297302 // This is a newly mounted hook
@@ -310,7 +315,6 @@ function createWorkInProgressHook(): Hook {
310315 workInProgressHook = workInProgressHook . next = hook ;
311316 } else {
312317 // There's already a work-in-progress. Reuse it.
313- isReRender = true ;
314318 workInProgressHook = workInProgressHook . next ;
315319 currentHook = currentHook !== null ? currentHook . next : null ;
316320 }
@@ -358,7 +362,7 @@ export function useReducer<S, A>(
358362 let queue : UpdateQueue < S , A > | null = ( workInProgressHook . queue : any ) ;
359363 if ( queue !== null ) {
360364 // Already have a queue, so this is an update.
361- if ( isReRender ) {
365+ if ( numberOfReRenders > 0 ) {
362366 // This is a re-render. Apply the new render phase updates to the previous
363367 // work-in-progress hook.
364368 const dispatch : Dispatch < A > = ( queue . dispatch : any ) ;
0 commit comments