You are enabling React Compiler in an existing Next.js app. Should it automatically consider eligible components, or only the functions you explicitly mark? That is what compilationMode: 'infer' versus 'annotation' controls. These are function-selection modes, not TypeScript typing strategies. (react.dev)
First, distinguish compilation modes from TypeScript types
In TypeScript, inference and annotation describe how a value gets its type:
let inferredCount = 0; // TypeScript infers number
let annotatedCount: number = 0; // You explicitly specify number
For a numeric counter, either declaration gives the variable the type number. Neither determines whether React Compiler selects a component. (typescriptlang.org)
React Compiler uses the same terms for a different decision. Consider this component:
function PriceTag({ amount }: { amount: number }) {
return <span>{amount}</span>;
}
With infer, its component-style name and JSX make it a compilation candidate. With annotation, it needs a "use memo" directive at the beginning of its body:
function PriceTag({ amount }: { amount: number }) {
'use memo';
return <span>{amount}</span>;
}
The amount: number annotation describes a prop’s type. The 'use memo' directive opts the function into React compilation in annotation mode. (react.dev)
For a staged rollout, suppose you want to evaluate a tested results panel before including checkout components. annotation lets you mark that panel while leaving unannotated functions outside compilation. infer instead selects candidates automatically using React naming and JSX/Hook heuristics. It is the default; you do not need to set it explicitly unless you want the policy visible in configuration. (react.dev)
Selection still does not guarantee successful compilation. A Next.js build can succeed while React Compiler skips functions it cannot safely optimize. Verify compilation, behavior, and performance separately. (react.dev)
Choose the default you want to review
compilationMode controls function selection. It does not establish that a selected function compiled successfully or that an interaction became faster. React’s default is infer; annotation requires an explicit "use memo" directive. (react.dev)
| Decision | infer | annotation |
|---|---|---|
| Selection | Recognizes component/hook naming plus JSX or Hook usage; also recognizes explicit directives | Selects explicitly annotated functions |
| New eligible component | Automatically considered | Requires an annotation |
| Suggested review model | Review exceptions and broadened coverage | Review each deliberate addition |
| Ongoing team cost | Investigate missed candidates and skips | Maintain annotations as code grows |
The selection behavior comes from React’s configuration reference; the review models are practical recommendations. (react.dev)
Choose annotation when you need a small, reviewable first change: a complicated application, uncertain third-party integrations, or limited regression coverage. The pull request can identify exactly which functions you intend to introduce to compilation.
Choose infer when you are comfortable considering eligible application code together and have enough testing to investigate regressions. It avoids making an annotation part of every future component’s adoption process.
Neither choice has to be permanent. React supports incremental adoption and temporary function-level opt-outs. (react.dev)
Start with the Next.js integration
The configuration below uses the top-level reactCompiler option, stabilized in Next.js 16. Install the compiler plugin as a development dependency, then configure the mode in next.config.ts. Check the documentation matching your installed Next.js version before applying this to an older app. (nextjs.org)
npm install -D babel-plugin-react-compiler
// next.config.ts
import type { NextConfig } from 'next';
const nextConfig: NextConfig = {
reactCompiler: {
compilationMode: 'annotation',
logger: {
logEvent(filename, event) {
console.log('[react-compiler]', filename, event.kind);
},
},
},
};
export default nextConfig;
This combines Next.js’s documented configuration with React’s logger API. The examples here have not been executed; validate them against your installed framework and compiler versions. The logger receives a filename, which may be null, and an event. Its event structure can change between compiler versions. (nextjs.org)
For automatic selection, change only the mode:
compilationMode: 'infer',
Keeping the setting explicit makes the rollout policy visible even though infer is the compiler default. (react.dev)
An annotation selects a function, not its whole subtree
Consider a client-side results panel. In annotation mode, mark the function you intend to evaluate:
'use client';
type Result = {
id: string;
title: string;
};
export function ResultsPanel({ results }: { results: Result[] }) {
'use memo';
return (
<ul>
{results.map(result => (
<ResultRow key={result.id} result={result} />
))}
</ul>
);
}
function ResultRow({ result }: { result: Result }) {
return <li>{result.title}</li>;
}
ResultsPanel is opted in. ResultRow is not independently opted in merely because the parent renders it. React’s incremental-adoption guidance requires annotations on each component and custom Hook you want selected in this mode. Keep "use memo" at the beginning of the function body. (react.dev)
That maintenance cost is the main reason not to leave an application in annotation mode by inertia. A new component can work correctly while remaining outside your intended rollout because nobody added its directive.
Read compiler events without inventing a coverage percentage
The logger distinguishes these useful outcomes:
CompileSuccess: a function compiled successfully.CompileError: a function was skipped because of errors.CompileSkip: a function was skipped for another reason.CompileDiagnostic: non-fatal diagnostic information.PipelineError: an unexpected compilation error.
These are compiler events, not runtime performance measurements. (react.dev)
For a first rollout, maintain a short list of intended functions and reconcile it with the build output. Investigate missing entries rather than treating silence as success. Start with filename and event kind; inspect the event details when something unexpected happens.
Do not calculate application coverage as successful events divided by all logged events. Diagnostics are not additional components, and a log alone does not define the population you intended to compile. Prefer a concrete statement: “The three functions selected for this change have confirmed compilation results.”
If you later automate reporting, validate the event schema against the installed compiler and make that validation part of compiler upgrades.
Keep memoization cleanup out of the first rollout
Do not combine enabling the compiler with deleting every useMemo, useCallback, and memo wrapper. Those are separate changes with separate explanations if behavior regresses.
There is also a configuration detail to check: Next.js documents environment.enablePreserveExistingMemoizationGuarantees. Its default depends on the active compiler implementation and version; the documentation recommends setting it explicitly for consistent behavior. Do not assume the selected compilation mode determines preservation policy. (nextjs.org)
Use separate changes:
- Enable compilation for the chosen scope while retaining existing memoization.
- Confirm compilation events and test the affected interactions.
- Evaluate individual memoization removals, checking behavior and performance again.
Pay particular attention to Effects and integrations that depend on object or callback identity. React’s troubleshooting guide identifies reliance on memoization for correctness as a source of runtime problems, including excessive Effect execution and missing updates. (react.dev)
For a suspected regression, temporarily place "use no memo" first in the affected function. In annotation mode, replace its "use memo" directive rather than stacking directives. Treat the opt-out as an investigation aid, with an issue and a removal condition. (react.dev)
Expand only after checking the interaction
For the results panel, compare the same interaction before and after: update an unrelated parent control, filter results, and select a row. Use equivalent data and build conditions. Check correctness first, then rendering work and interaction timing.
Update performance is a relevant target: React describes the compiler’s automatic memoization as primarily focused on re-rendering existing components and avoiding repeated calculations during rendering. Compilation alone does not supply a measured improvement for your application. (react.dev)
A useful rollout record has three independent fields:
| Check | Evidence to record |
|---|---|
| Selection and compilation | Intended function and observed compiler result |
| Correctness | Tests and interactions checked |
| Performance | Comparable measurement, including no detectable improvement |
Stay with annotation while reviewing individual additions is useful. Move to infer when maintaining the opt-in list costs more than reviewing broader selection and its exceptions. Make that switch its own rollout change: newly eligible functions deserve the same checks as the first annotated ones.
THE CONVERSATION
Comments
Questions, useful examples and different approaches are welcome. Keep the discussion thoughtful and on topic.
Loading comments…