Skip to content

Commit d2367f1

Browse files
authored
[rcr] Reexport React.__COMPILER_RUNTIME.c or fallback to polyfill (#31140)
This PR updates the standalone `react-compiler-runtime` package to either re-export `React.__COMPILER_RUNTIME.c` or to use a userspace polyfill.
1 parent b78a7f2 commit d2367f1

File tree

1 file changed

+21
-16
lines changed
  • compiler/packages/react-compiler-runtime/src

1 file changed

+21
-16
lines changed

compiler/packages/react-compiler-runtime/src/index.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,27 @@ const ReactSecretInternals =
1919
type MemoCache = Array<number | typeof $empty>;
2020

2121
const $empty = Symbol.for('react.memo_cache_sentinel');
22-
/**
23-
* DANGER: this hook is NEVER meant to be called directly!
24-
**/
25-
export function c(size: number) {
26-
return React.useState(() => {
27-
const $ = new Array(size);
28-
for (let ii = 0; ii < size; ii++) {
29-
$[ii] = $empty;
30-
}
31-
// This symbol is added to tell the react devtools that this array is from
32-
// useMemoCache.
33-
// @ts-ignore
34-
$[$empty] = true;
35-
return $;
36-
})[0];
37-
}
22+
23+
// Re-export React.c if present, otherwise fallback to the userspace polyfill for versions of React
24+
// < 19.
25+
export const c =
26+
// @ts-expect-error
27+
typeof React.__COMPILER_RUNTIME?.c === 'function'
28+
? // @ts-expect-error
29+
React.__COMPILER_RUNTIME.c
30+
: function c(size: number) {
31+
return React.useMemo<Array<unknown>>(() => {
32+
const $ = new Array(size);
33+
for (let ii = 0; ii < size; ii++) {
34+
$[ii] = $empty;
35+
}
36+
// This symbol is added to tell the react devtools that this array is from
37+
// useMemoCache.
38+
// @ts-ignore
39+
$[$empty] = true;
40+
return $;
41+
}, []);
42+
};
3843

3944
const LazyGuardDispatcher: {[key: string]: (...args: Array<any>) => any} = {};
4045
[

0 commit comments

Comments
 (0)