Using the compiler on < React 19 #6
Replies: 4 comments 11 replies
-
Regarding this, would it be possible, reasonable, or worth it to re-create React's built-in implementation? Wouldn't expect it to be supported, but if we won't be able to update to R19 for bit, it might be worth investing in an improved polyfill. |
Beta Was this translation helpful? Give feedback.
-
I see that there is a |
Beta Was this translation helpful? Give feedback.
-
Please note that you no longer need the previously linked gist, you can follow the docs on how to get the compiler to work with React <19. |
Beta Was this translation helpful? Give feedback.
-
React 17 and 18 are now officially supported, so these workarounds are no longer needed. Please see our React Compiler Beta release post for more information. |
Beta Was this translation helpful? Give feedback.
-
tl;dr:
_c
is a hook for use by the React Compiler, don't use it directly. If you are unable to upgrade to React 19, please installreact-compiler-runtime
.Overview
React Compiler automatically memoizes values, but does not use useMemo or useCallback. Since the compiler can memoize a lot more than was previously possible with manual memoization, the overhead of allocating the closure and dependency array for useMemo/useCallback can become sizable.
Instead, the compiler directly inlines the conditional check for the dependencies, caching the value and reusing the cached value if the dependencies are the same.
For example, instead of a useMemo call like this:
The compiler would generate code like this:
Notice the
$
in the generated code, this$
is the array of cached values and it's dependencies. It is created by calling an internal React function (here, named_c
). This "cache function" is unsafe to use and not part of the public API, and is only meant as a target for React Compiler specifically.This API is included in React 19, but it is possible to provide a userspace implementation.
react-compiler-runtime
If you are unable to upgrade to React 19, you can install
react-compiler-runtime
, and add your React version target to your config. Acceptable values are'17' | '18' | '19'
with19
being the default.Usage
The memo cache function exported by React should not be used directly, as we will make breaking changes to this API over time.
Beta Was this translation helpful? Give feedback.
All reactions