@@ -105,7 +105,6 @@ import {
105105 requestEventTime ,
106106 markSkippedUpdateLanes ,
107107 isInvalidExecutionContextForEventFunction ,
108- getSuspendedThenableState ,
109108} from './ReactFiberWorkLoop.new' ;
110109
111110import getComponentNameFromFiber from 'react-reconciler/src/getComponentNameFromFiber' ;
@@ -141,9 +140,9 @@ import {
141140import { getTreeId } from './ReactFiberTreeContext.new' ;
142141import { now } from './Scheduler' ;
143142import {
144- prepareThenableState ,
145143 trackUsedThenable ,
146144 checkIfUseWrappedInTryCatch ,
145+ createThenableState ,
147146} from './ReactFiberThenable.new' ;
148147import type { ThenableState } from './ReactFiberThenable.new' ;
149148
@@ -247,6 +246,7 @@ let shouldDoubleInvokeUserFnsInHooksDEV: boolean = false;
247246let localIdCounter : number = 0 ;
248247// Counts number of `use`-d thenables
249248let thenableIndexCounter : number = 0 ;
249+ let thenableState : ThenableState | null = null ;
250250
251251// Used for ids that are generated completely client-side (i.e. not during
252252// hydration). This counter is global, so client ids are not stable across
@@ -449,6 +449,7 @@ export function renderWithHooks<Props, SecondArg>(
449449 // didScheduleRenderPhaseUpdate = false;
450450 // localIdCounter = 0;
451451 // thenableIndexCounter = 0;
452+ // thenableState = null;
452453
453454 // TODO Warn if no hooks are used at all during mount, then some are used during update.
454455 // Currently we will identify the update render as a mount because memoizedState === null.
@@ -477,10 +478,6 @@ export function renderWithHooks<Props, SecondArg>(
477478 : HooksDispatcherOnUpdate ;
478479 }
479480
480- // If this is a replay, restore the thenable state from the previous attempt.
481- const prevThenableState = getSuspendedThenableState ( ) ;
482- prepareThenableState ( prevThenableState ) ;
483-
484481 // In Strict Mode, during development, user functions are double invoked to
485482 // help detect side effects. The logic for how this is implemented for in
486483 // hook components is a bit complex so let's break it down.
@@ -525,7 +522,6 @@ export function renderWithHooks<Props, SecondArg>(
525522 Component ,
526523 props ,
527524 secondArg ,
528- prevThenableState ,
529525 ) ;
530526 }
531527
@@ -538,7 +534,6 @@ export function renderWithHooks<Props, SecondArg>(
538534 Component ,
539535 props ,
540536 secondArg ,
541- prevThenableState ,
542537 ) ;
543538 } finally {
544539 setIsStrictModeForDevtools ( false ) ;
@@ -600,7 +595,9 @@ function finishRenderingHooks(current: Fiber | null, workInProgress: Fiber) {
600595 didScheduleRenderPhaseUpdate = false ;
601596 // This is reset by checkDidRenderIdHook
602597 // localIdCounter = 0;
598+
603599 thenableIndexCounter = 0 ;
600+ thenableState = null ;
604601
605602 if ( didRenderTooFewHooks ) {
606603 throw new Error (
@@ -652,7 +649,6 @@ export function replaySuspendedComponentWithHooks<Props, SecondArg>(
652649 Component : ( p : Props , arg : SecondArg ) => any ,
653650 props : Props ,
654651 secondArg : SecondArg ,
655- prevThenableState : ThenableState | null ,
656652) : any {
657653 // This function is used to replay a component that previously suspended,
658654 // after its data resolves.
@@ -676,7 +672,6 @@ export function replaySuspendedComponentWithHooks<Props, SecondArg>(
676672 Component,
677673 props,
678674 secondArg,
679- prevThenableState,
680675 ) ;
681676 finishRenderingHooks ( current , workInProgress ) ;
682677 return children;
@@ -687,7 +682,6 @@ function renderWithHooksAgain<Props, SecondArg>(
687682 Component : ( p : Props , arg : SecondArg ) = > any ,
688683 props : Props ,
689684 secondArg : SecondArg ,
690- prevThenableState : ThenableState | null ,
691685) {
692686 // This is used to perform another render pass. It's used when setState is
693687 // called during render, and for double invoking components in Strict Mode
@@ -735,7 +729,6 @@ function renderWithHooksAgain<Props, SecondArg>(
735729 ? HooksDispatcherOnRerenderInDEV
736730 : HooksDispatcherOnRerender ;
737731
738- prepareThenableState ( prevThenableState ) ;
739732 children = Component ( props , secondArg ) ;
740733 } while ( didScheduleRenderPhaseUpdateDuringThisPass ) ;
741734 return children ;
@@ -821,6 +814,7 @@ export function resetHooksOnUnwind(): void {
821814 didScheduleRenderPhaseUpdateDuringThisPass = false ;
822815 localIdCounter = 0 ;
823816 thenableIndexCounter = 0 ;
817+ thenableState = null ;
824818}
825819
826820function mountWorkInProgressHook ( ) : Hook {
@@ -954,7 +948,11 @@ function use<T>(usable: Usable<T>): T {
954948 // Track the position of the thenable within this fiber.
955949 const index = thenableIndexCounter ;
956950 thenableIndexCounter += 1 ;
957- return trackUsedThenable ( thenable , index ) ;
951+
952+ if ( thenableState === null ) {
953+ thenableState = createThenableState ( ) ;
954+ }
955+ return trackUsedThenable ( thenableState , thenable , index ) ;
958956 } else if (
959957 usable.$$typeof === REACT_CONTEXT_TYPE ||
960958 usable.$$typeof === REACT_SERVER_CONTEXT_TYPE
0 commit comments