[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.
lauren committed
Oct 7, 2024 at 16:31 UTC
d2367f17d9cac8f0325660455beff60a82b618d4
1 file changed
+21
-16
compiler/packages/react-compiler-runtime/src/index.ts
+21
-16
@@ -19,22 +19,27 @@ const ReactSecretInternals =
19
type MemoCache = Array<number | typeof $empty>;
20
21
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
+ };
43
44
const LazyGuardDispatcher: {[key: string]: (...args: Array<any>) => any} = {};
45
[