Course contentsShow
← React
Lesson 1506 of 2,39637. React 19 and BeyondFree lesson

When the Compiler Can't Optimize

Edge cases where manual memoization is still needed: refs, external mutations, and non-reactive dependencies.

When the Compiler Can't Optimize

You'll learn when React's compiler hits its limits and manual memoization is still required.

The React Compiler is incredibly smart at detecting reactive dependencies and automatically optimizing your components. However, there are edge cases where it can't safely optimize your code, and you'll need to fall back to manual useMemo, useCallback, or memo.

Refs Don't Trigger Re-renders

The compiler tracks state and props because they're reactive—changes cause re-renders. But refs are invisible to the reactive system. If you're doing complex calculations based on ref.current, the compiler can't know when that value changes:

function Canvas() {
  const canvasRef = useRef(null);
  
  // Compiler can't optimize this—ref changes don't trigger renders
  const expensiveCalculation = computeLayout(canvasRef.current);
  
  return <canvas ref={canvasRef} />;
}

You'd still need useMemo with proper dependencies here.

External Mutations

If your component reads from external global state (not managed by React), the compiler has no visibility:

let externalCache = {};

function DataDisplay() {
  // Compiler can't track externalCache changes
  const processed = processData(externalCache.data);
  
  return <div>{processed}</div>;
}

Non-Reactive Dependencies

Similarly, values from external libraries, browser APIs, or anything outside React's reactivity model can't be tracked. The compiler optimizes React's reactive graph—it doesn't instrument the entire JavaScript runtime.

Think of it like a smart home system: it can automate lights and thermostats it's connected to, but it can't control your neighbor's house or manual switches it doesn't know about.

Key Takeaway: The React Compiler optimizes reactive React code beautifully, but refs, external mutations, and non-React data sources still require manual memoization because they live outside React's reactive dependency graph.