~/alexsh.blog

$ cat react-internals/10-react-compiler.md

React Compiler

February 9, 20261 min readpart 10/13

The React Compiler (originally "React Forget") is an AOT compiler that automatically memoizes components and hooks at build time. It tracks which variables depend on which reactive inputs and groups them into reactive scopes:

codetsx
// What you write:
function TodoList({ todos, filter }) {
  const filtered = todos.filter(t => t.status === filter);
  return <List items={filtered} />;
}

// What the compiler outputs (conceptually):
function TodoList({ todos, filter }) {
  const $ = useMemoCache(4);
  if ($[0] !== todos || $[1] !== filter) {
    $[2] = todos.filter(t => t.status === filter);
    $[0] = todos;
    $[1] = filter;
  }
  return $[2];
}

Further reading