Component Purity Analysis in the React Compiler

Component Purity Analysis is part of how the React Compiler reasons about whether component and Hook code behaves predictably enough for optimization. It checks for patterns that can violate React's purity expectations, because unpredictable render-time behavior can make compiler transformations unsafe.

React components and Hooks are expected to be pure during rendering: given the same inputs and relevant state, rendering should not produce externally visible side effects or mutate values that React relies on. The React Compiler uses static analysis to reason about these expectations before applying optimizations.

The analysis can detect problematic patterns such as mutating props or state-derived values, performing side effects during render, or using values in ways that make dependencies and behavior difficult to reason about. These findings can prevent optimization or produce diagnostics that point to code that should be changed.

Purity does not mean a component must have no effects anywhere in its implementation. User interactions and lifecycle-related work can perform side effects, but those operations should occur in appropriate places such as event handlers or Effects rather than during the render calculation itself.

For an interview, distinguish compiler analysis from runtime enforcement. The compiler examines source code and determines whether it can safely transform code; it does not turn JavaScript into a pure language or guarantee that every possible impurity will be detected.

Key points

Interview questions

What does component purity mean in React?

A pure component treats rendering as a calculation from its inputs rather than as a place to perform external side effects. It should not mutate props, state, or other render-relevant data while calculating its output. This predictability lets React reason about rendering and enables compiler optimizations. Side effects should instead happen in event handlers or appropriate Effects.

How does Component Purity Analysis relate to the React Compiler?

The React Compiler analyzes component and Hook code to determine whether it satisfies assumptions required for safe optimization. Purity is important because transformations may rely on render computations being predictable and free of unexpected mutation or side effects. When the compiler encounters code it cannot safely reason about, it can avoid optimizing that code or report a diagnostic. This makes purity analysis part of the compiler's safety reasoning rather than a runtime feature.

Why is mutating a prop during render problematic?

Props are inputs supplied to a component, so changing them during rendering breaks the expectation that rendering reads its inputs rather than modifies them. Other code may still hold a reference to the same object and observe the unexpected change. Such mutation also makes compiler transformations harder to reason about because the apparent inputs to a computation can change as a consequence of that computation. Creating a derived value instead preserves the input and keeps rendering predictable.

Does purity analysis mean React components cannot perform side effects?

No. The key boundary is the render phase rather than the entire component module. React applications routinely perform side effects, such as subscriptions, network synchronization, and DOM integration, but those operations should be placed in mechanisms designed for them, such as event handlers or Effects. The compiler needs render-time behavior to be sufficiently predictable to transform it safely. Therefore, moving an operation out of render can change whether it is compatible with compiler optimization.

What should you do when the React Compiler reports a purity-related diagnostic?

First, identify the render-time operation that violates the compiler's assumptions rather than simply suppressing the diagnostic. Look for mutation, external side effects, unstable access patterns, or other code that prevents the compiler from establishing safe behavior. Refactor the component so rendering computes its result without modifying external state, and move synchronization or interaction work to the appropriate React mechanism. If a pattern is intentionally incompatible with optimization, it may be appropriate to leave that code unoptimized while keeping the rest of the application eligible for compilation.