@@ -15,6 +15,7 @@ import type {
1515 ReactProviderType ,
1616 StartTransitionOptions ,
1717} from 'shared/ReactTypes' ;
18+ import { REACT_MEMO_CACHE_SENTINEL } from 'shared/ReactSymbols' ;
1819import type {
1920 Fiber ,
2021 Dispatcher as DispatcherType ,
@@ -51,9 +52,19 @@ type Dispatch<A> = A => void;
5152
5253let primitiveStackCache : null | Map < string , Array < any >> = null ;
5354
55+ type MemoCache = {
56+ data : Array < Array < any >> ,
57+ index : number ,
58+ } ;
59+
60+ type FunctionComponentUpdateQueue = {
61+ memoCache ?: MemoCache | null ,
62+ } ;
63+
5464type Hook = {
5565 memoizedState : any ,
5666 next : Hook | null ,
67+ updateQueue : FunctionComponentUpdateQueue | null ,
5768} ;
5869
5970function getPrimitiveStackCache ( ) : Map < string , Array < any >> {
@@ -79,6 +90,10 @@ function getPrimitiveStackCache(): Map<string, Array<any>> {
7990 Dispatcher . useDebugValue ( null ) ;
8091 Dispatcher . useCallback ( ( ) => { } ) ;
8192 Dispatcher . useMemo ( ( ) => null ) ;
93+ if ( typeof Dispatcher . useMemoCache === 'function' ) {
94+ // This type check is for Flow only.
95+ Dispatcher . useMemoCache ( 0 ) ;
96+ }
8297 } finally {
8398 readHookLog = hookLog ;
8499 hookLog = [ ] ;
@@ -326,6 +341,37 @@ function useId(): string {
326341 return id ;
327342}
328343
344+ function useMemoCache(size: number): Array< any > {
345+ const hook = nextHook ( ) ;
346+ let memoCache : MemoCache ;
347+ if (
348+ hook !== null &&
349+ hook . updateQueue !== null &&
350+ hook . updateQueue . memoCache != null
351+ ) {
352+ memoCache = hook . updateQueue . memoCache ;
353+ } else {
354+ memoCache = {
355+ data : [ ] ,
356+ index : 0 ,
357+ } ;
358+ }
359+
360+ let data = memoCache . data [ memoCache . index ] ;
361+ if ( data === undefined ) {
362+ data = new Array ( size ) ;
363+ for ( let i = 0 ; i < size ; i ++ ) {
364+ data [ i ] = REACT_MEMO_CACHE_SENTINEL ;
365+ }
366+ }
367+ hookLog . push ( {
368+ primitive : 'MemoCache' ,
369+ stackError : new Error ( ) ,
370+ value : data ,
371+ } ) ;
372+ return data ;
373+ }
374+
329375const Dispatcher: DispatcherType = {
330376 readContext ,
331377 useCacheRefresh ,
@@ -337,6 +383,7 @@ const Dispatcher: DispatcherType = {
337383 useLayoutEffect ,
338384 useInsertionEffect ,
339385 useMemo ,
386+ useMemoCache ,
340387 useReducer ,
341388 useRef ,
342389 useState ,
0 commit comments