From ae743ae59f8d16640877b390feb21980cc9375ed Mon Sep 17 00:00:00 2001 From: Lauren Tan Date: Tue, 24 Sep 2024 18:43:37 -0400 Subject: [PATCH] Update [ghstack-poisoned] --- .../react-compiler-runtime/src/index.ts | 37 +++++++++++-------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/compiler/packages/react-compiler-runtime/src/index.ts b/compiler/packages/react-compiler-runtime/src/index.ts index cefc934ae1d67..819d3e9ef0643 100644 --- a/compiler/packages/react-compiler-runtime/src/index.ts +++ b/compiler/packages/react-compiler-runtime/src/index.ts @@ -19,22 +19,27 @@ const ReactSecretInternals = type MemoCache = Array; const $empty = Symbol.for('react.memo_cache_sentinel'); -/** - * DANGER: this hook is NEVER meant to be called directly! - **/ -export function c(size: number) { - return React.useState(() => { - const $ = new Array(size); - for (let ii = 0; ii < size; ii++) { - $[ii] = $empty; - } - // This symbol is added to tell the react devtools that this array is from - // useMemoCache. - // @ts-ignore - $[$empty] = true; - return $; - })[0]; -} + +// Re-export React.c if present, otherwise fallback to the userspace polyfill for versions of React +// < 19. +export const c = + // @ts-expect-error + typeof React.c === 'function' + ? // @ts-expect-error + React.c + : function c(size: number) { + return React.useState>(() => { + const $ = new Array(size); + for (let ii = 0; ii < size; ii++) { + $[ii] = $empty; + } + // This symbol is added to tell the react devtools that this array is from + // useMemoCache. + // @ts-ignore + $[$empty] = true; + return $; + })[0]; + }; export function $read(memoCache: MemoCache, index: number) { const value = memoCache[index];