Skip to content

React Compiler Breaking Code — Fix Opt-Out with 'use no memo'

DodaTech Updated 2026-06-24 2 min read

In this fix, you'll learn when and how to opt out of React Compiler. This matters for avoiding compiler bugs with ref-based patterns, and appears in Canvas/WebGL components and third-party integrations.

The Problem

The following code demonstrates the typical mistake:

function Canvas() {
  const canvasRef = useRef(null);
  useEffect(() => {
    const ctx = canvasRef.current.getContext('2d');
  }, []);
  return <canvas ref={canvasRef} />;
}
// Compiler optimization may break imperative canvas logic

The Fix

Here is the corrected version:

'use no memo';
function Canvas() {
  const canvasRef = useRef(null);
  useEffect(() => {
    const ctx = canvasRef.current.getContext('2d');
  }, []);
  return <canvas ref={canvasRef} />;
}
// Component works correctly with compiler disabled

Prevention

  • Add 'use no memo' at the top of files relying on mutable refs or imperative DOM APIs.
  • Use the directive sparingly — only when you identify a compiler-caused bug.
  • The directive applies to the entire file below it.
  • Components using forwardRef, useImperativeHandle, or Canvas/WebGL often need opt-out.
  • Test thoroughly with compiler enabled before excluding components.

Common Mistakes with compiler opt out

  1. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  2. Misunderstanding that String is [Char] with poor performance for large text operations
  3. Using foldl instead of foldl' causing stack overflow on large lists

These mistakes appear frequently in real-world REACT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### When should I opt out of the React Compiler?

When the component uses imperative DOM APIs (Canvas, WebGL, Three.js), relies on ref mutations, uses libraries with side effects the compiler can't analyze, or produces incorrect results.

Does 'use no memo' affect child components?

No — only the file where it's placed. Extract opt-out components to their own file if needed.

How do I know if the compiler is causing a bug?

If a component works without the compiler but breaks with it enabled, the compiler is likely the cause. Common signs: stale closures, effects not re-running, unexpected ref behavior.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro