@@ -175,34 +175,10 @@ function useReducer<S, I, A>(
175175 const id = getCurrentIdentity ( )
176176 workInProgressHook = createWorkInProgressHook ( )
177177
178- if ( isReRender ) {
179- // This is a re-render. Apply the new render phase updates to the previous
180- // current hook.
181- const queue : UpdateQueue < A > = ( workInProgressHook . queue : any )
182- const dispatch : Dispatch < A > = ( queue . dispatch : any )
183- if ( renderPhaseUpdates !== null ) {
184- // Render phase updates are stored in a map of queue -> linked list
185- const firstRenderPhaseUpdate = renderPhaseUpdates . get ( queue )
186- if ( firstRenderPhaseUpdate !== undefined ) {
187- renderPhaseUpdates . delete ( queue )
188- let newState = workInProgressHook . memoizedState
189- let update = firstRenderPhaseUpdate
190- do {
191- // Process this render phase update. We don't have to check the
192- // priority because it will always be the same as the current
193- // render's.
194- const action = update . action
195- newState = reducer ( newState , action )
196- update = update . next
197- } while ( update !== null )
198-
199- workInProgressHook . memoizedState = newState
200-
201- return [ newState , dispatch ]
202- }
203- }
204- return [ workInProgressHook . memoizedState , dispatch ]
205- } else {
178+ // In the case of a re-render after a suspense, the initial state
179+ // may not be set, so instead of initialising if `!isRerender`, we
180+ // check whether `queue` is set
181+ if ( workInProgressHook . queue === null ) {
206182 let initialState
207183 if ( reducer === basicStateReducer ) {
208184 // Special case for `useState`.
@@ -214,18 +190,39 @@ function useReducer<S, I, A>(
214190 initialState =
215191 init !== undefined ? init ( initialArg ) : ( ( initialArg : any ) : S )
216192 }
193+
217194 workInProgressHook.memoizedState = initialState
218- const queue: UpdateQueue< A > = (workInProgressHook.queue = {
219- last : null ,
220- dispatch : null
221- } )
222- const dispatch: Dispatch< A > = (queue.dispatch = (dispatchAction.bind(
223- null,
224- id,
225- queue
226- ): any))
227- return [workInProgressHook.memoizedState, dispatch]
228195 }
196+
197+ const queue : UpdateQueue < A > =
198+ workInProgressHook.queue ||
199+ (workInProgressHook.queue = { last : null , dispatch : null } )
200+ const dispatch: Dispatch< A > =
201+ queue.dispatch || (queue.dispatch = dispatchAction.bind(null, id, queue))
202+
203+ if (isReRender && renderPhaseUpdates !== null ) {
204+ // This is a re-render. Apply the new render phase updates to the previous
205+ // current hook.
206+ // Render phase updates are stored in a map of queue -> linked list
207+ const firstRenderPhaseUpdate = renderPhaseUpdates . get ( queue )
208+ if ( firstRenderPhaseUpdate !== undefined ) {
209+ renderPhaseUpdates . delete ( queue )
210+ let newState = workInProgressHook . memoizedState
211+ let update = firstRenderPhaseUpdate
212+ do {
213+ // Process this render phase update. We don't have to check the
214+ // priority because it will always be the same as the current
215+ // render's.
216+ const action = update . action
217+ newState = reducer ( newState , action )
218+ update = update . next
219+ } while (update !== null)
220+
221+ workInProgressHook.memoizedState = newState
222+ }
223+ }
224+
225+ return [ workInProgressHook . memoizedState , dispatch ]
229226}
230227
231228function useMemo < T > (nextCreate: () => T , deps : Array < mixed > | void | null): T {
0 commit comments