-
-
Save poteto/37c076bf112a07ba39d0e5f0645fec43 to your computer and use it in GitHub Desktop.
[DEPRECATED] React Compiler compatibility module for codebases unable to use React 19
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = function(api) { | |
| return { | |
| plugins: [ | |
| [ | |
| 'babel-plugin-react-compiler', { | |
| runtimeModule: 'react-compiler-runtime' | |
| } | |
| ] | |
| ] | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "dependencies": { | |
| "react-compiler-runtime": "file:./lib/react-compiler-runtime" | |
| }, | |
| "devDependencies": { | |
| "babel-plugin-react-compiler": "latest" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // lib/react-compiler-runtime/index.js | |
| import React from 'react'; | |
| const $empty = Symbol.for("react.memo_cache_sentinel"); | |
| /** | |
| * DANGER: this hook is NEVER meant to be called directly! | |
| * | |
| * Note that this is a temporary userspace implementation of this function | |
| * from React 19. It is not as efficient and may invalidate more frequently | |
| * than the official API. Please upgrade to React 19 as soon as you can. | |
| **/ | |
| export function c(size) { | |
| return React.useState(() => { | |
| const $ = new Array(size); | |
| for (let ii = 0; ii < size; ii++) { | |
| $[ii] = $empty; | |
| } | |
| // @ts-ignore | |
| $[$empty] = true; | |
| return $; | |
| })[0]; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // lib/react-compiler-runtime/package.json | |
| { | |
| "name": "react-compiler-runtime", | |
| "version": "0.0.1", | |
| "license": "MIT", | |
| "main": "index.js", | |
| "dependencies": { | |
| "react": "^18.2.0" | |
| } | |
| } |
@ferrybig ah yes, of course — I am so used to seeing it written with the destructuring syntax that I was blind to that — thanks
nit:
12 export function c(size: number) {
has a : number type annotation, which is not javascript
Author
updated gist to esm and removed type annotations
Author
Please note that you no longer need this gist, you can follow the docs on how to get the compiler to work with React <19.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@jaeschliman With the react function
useState, it returns a tuple containing 2 values. The first one is the state, the second one is a setter for the state. We are only interested in the state from theuseStateand discard the setter. Note that it is rare to not use the setter, most of the time when you useuseState, you actually want to be able to change the state, not just hereMore reading of the
useStatehook: https://react.dev/reference/react/useState