Skip to content

Instantly share code, notes, and snippets.

@poteto
Last active January 21, 2026 00:46
Show Gist options
  • Select an option

  • Save poteto/37c076bf112a07ba39d0e5f0645fec43 to your computer and use it in GitHub Desktop.

Select an option

Save poteto/37c076bf112a07ba39d0e5f0645fec43 to your computer and use it in GitHub Desktop.
[DEPRECATED] React Compiler compatibility module for codebases unable to use React 19
module.exports = function(api) {
return {
plugins: [
[
'babel-plugin-react-compiler', {
runtimeModule: 'react-compiler-runtime'
}
]
]
}
}
{
"dependencies": {
"react-compiler-runtime": "file:./lib/react-compiler-runtime"
},
"devDependencies": {
"babel-plugin-react-compiler": "latest"
}
}
// 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];
}
// lib/react-compiler-runtime/package.json
{
"name": "react-compiler-runtime",
"version": "0.0.1",
"license": "MIT",
"main": "index.js",
"dependencies": {
"react": "^18.2.0"
}
}
@undesicimo

Copy link
Copy Markdown

The polyfill doesn't seem to work on React >18. We get

Error: Missing "./compiler-runtime" specifier in "react" package
    at e 

Works perfect in React 17.0.2 though

@iahu

iahu commented May 28, 2024

Copy link
Copy Markdown

CSS Module not works in Vite HMR mode

@iahu

iahu commented May 28, 2024

Copy link
Copy Markdown

The polyfill doesn't seem to work on React >18. We get

Error: Missing "./compiler-runtime" specifier in "react" package
    at e 

Works perfect in React 17.0.2 though

make sure you are not using the "4.3.0" version @vitejs/plugin-react, try to use "4.2.1"

@undesicimo

Copy link
Copy Markdown

@iahu
downgrading vite plugin worked! cheers

@jsonz1993

Copy link
Copy Markdown

#file-react-compiler-runtime-index-js-L13
What's the difference between using React.useState and returning an normal Array ?
I tried Array to work as well too

@brandonpapworth

brandonpapworth commented Jun 18, 2024

Copy link
Copy Markdown

+1ing @jsonz1993

Middle ground may even be using useRef to keep it safely contained and consistent.

// lib/react-compiler-runtime/index.js

const { useRef } = require('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: number): any[] {
  const memoArrayRef = useRef<any[] | undefined>(undefined)

  if (!memoArrayRef.value) {
    memoArrayRef.value = []
    for (let i = 0; i < size; i += 1) {
      memoArrayRef.value.push($empty)
    }
    // @ts-ignore
    memoArrayRef.value[$empty] = true;
  }

  return memoArrayRef.value
}

@jaeschliman

Copy link
Copy Markdown

@poteto I'm curious, why does the polyfill appear to return the first element of the created array instead of the array itself? surely that is a mistake?

@ferrybig

Copy link
Copy Markdown

I'm curious, why does the polyfill appear to return the first element of the created array instead of the array itself? surely that is a mistake?

@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 the useState and discard the setter. Note that it is rare to not use the setter, most of the time when you use useState, you actually want to be able to change the state, not just here

More reading of the useState hook: https://react.dev/reference/react/useState

@jaeschliman

jaeschliman commented Jul 15, 2024 via email

Copy link
Copy Markdown

@ashubham

Copy link
Copy Markdown

nit:

12 export function c(size: number) {

has a : number type annotation, which is not javascript

@poteto

poteto commented Jul 23, 2024

Copy link
Copy Markdown
Author

updated gist to esm and removed type annotations

@poteto

poteto commented Oct 7, 2024

Copy link
Copy Markdown
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