Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readReact

Why React Compiler 1.0 skips components

Read React Compiler diagnostics in ESLint, then check the build log to confirm which components compile after you fix the reported line.

You turn on React Compiler and open a price list in React DevTools. Neighboring components show a Memo badge, but PriceList runs as before. Look for a compiler diagnostic before changing the build.

Find the line in ESLint

The recommended eslint-plugin-react-hooks configuration includes compiler diagnostics. Run your linter on the affected file. With the compiler's default panicThreshold: 'none', a diagnostic can skip one component without stopping compilation elsewhere in the file.

Save this example as price-list.jsx in a project whose ESLint config parses JSX and enables the React Hooks recommended rules.

import { useRef } from 'react'

export function PriceList({ items }) {
  const renders = useRef(0)
  renders.current += 1
  return (
    <ul data-renders={renders.current}>
      {items.map(({ id, name }) => <li key={id}>{name}</li>)}
    </ul>
  )
}

The react-hooks/refs rule reports Cannot access refs during render. Check both the increment and the read in data-renders.

The counter changes a ref each time React calls the component. React can discard a render, so that write can count work that never reaches the screen. Remove the counter and the data-renders attribute. The list renders from items alone, and the ref diagnostic disappears. Use React DevTools Profiler to see which renders reached a commit.

Log the build's decision

ESLint points to the code you can edit. To check what the build did with that edit, pass these options to the compiler plugin, then rerun the build with the same entry points.

export const compilerOptions = {
  logger: {
    logEvent(filename, event) {
      if (event.kind === 'CompileError') {
        console.warn(filename, event.detail.reason)
      }
      if (event.kind === 'CompileSuccess') {
        console.log(filename, event.fnName, 'compiled')
      }
    },
  },
}

The broken file logs Cannot access refs during render, and the edited file logs PriceList compiled.

The compiler logger reports events by filename. Read the reason for each failure before checking for a success event for the function you changed.

If you see no events for the file, check that your bundler runs the plugin on it. Compare with a component in the same build that you know compiles. Check any include or exclude filters before you rewrite the component.

The React Compiler 1.0 release notes describe what the lint integration reports. Fix one diagnostic and run the same file through the linter and build again. For a component that already compiles and still has manual memoization, audit the existing dependencies as a separate change.