React Compiler directives are special string literals that control whether specific functions are compiled.
function MyComponent() { "use memo"; // Opt this component into compilation return <div>{/* ... */}</div>;}Overview
Section titled “Overview”React Compiler directives provide fine-grained control over which functions are optimized by the compiler. They are string literals placed at the beginning of a function body or at the top of a module.
Available directives
Section titled “Available directives”"use memo"- Opts a function into compilation"use no memo"- Opts a function out of compilation
Quick comparison
Section titled “Quick comparison”| Directive | Purpose | When to use |
|---|---|---|
"use memo" | Force compilation | When using annotation mode or to override infer mode heuristics |
"use no memo" | Prevent compilation | Debugging issues or working with incompatible code |
Function-level directives
Section titled “Function-level directives”Place directives at the beginning of a function to control its compilation:
// Opt into compilationfunction OptimizedComponent() { "use memo"; return <div>This will be optimized</div>;}
// Opt out of compilationfunction UnoptimizedComponent() { "use no memo"; return <div>This won't be optimized</div>;}Module-level directives
Section titled “Module-level directives”Place directives at the top of a file to affect all functions in that module:
// At the very top of the file"use memo";
// All functions in this file will be compiledfunction Component1() { return <div>Compiled</div>;}
function Component2() { return <div>Also compiled</div>;}
// Can be overridden at function levelfunction Component3() { "use no memo"; // This overrides the module directive return <div>Not compiled</div>;}Compilation modes interaction
Section titled “Compilation modes interaction”Directives behave differently depending on your compilationMode:
annotationmode: Only functions with"use memo"are compiledinfermode: Compiler decides what to compile, directives override decisionsallmode: Everything is compiled,"use no memo"can exclude specific functions
Best practices
Section titled “Best practices”Use directives sparingly
Section titled “Use directives sparingly”Directives are escape hatches. Prefer configuring the compiler at the project level:
// ✅ Good - project-wide configuration{ plugins: [ ['babel-plugin-react-compiler', { compilationMode: 'infer' }] ]}
// ⚠️ Use directives only when neededfunction SpecialCase() { "use no memo"; // Document why this is needed // ...}Document directive usage
Section titled “Document directive usage”Always explain why a directive is used:
// ✅ Good - clear explanationfunction DataGrid() { "use no memo"; // TODO: Remove after fixing issue with dynamic row heights (JIRA-123) // Complex grid implementation}
// ❌ Bad - no explanationfunction Mystery() { "use no memo"; // ...}Plan for removal
Section titled “Plan for removal”Opt-out directives should be temporary:
- Add the directive with a TODO comment
- Create a tracking issue
- Fix the underlying problem
- Remove the directive
function TemporaryWorkaround() { "use no memo"; // TODO: Remove after upgrading ThirdPartyLib to v2.0 return <ThirdPartyComponent />;}Common patterns
Section titled “Common patterns”Gradual adoption
Section titled “Gradual adoption”When adopting the React Compiler in a large codebase:
// Start with annotation mode{ compilationMode: 'annotation'}
// Opt in stable componentsfunction StableComponent() { "use memo"; // Well-tested component}
// Later, switch to infer mode and opt out problematic onesfunction ProblematicComponent() { "use no memo"; // Fix issues before removing // ...}Troubleshooting
Section titled “Troubleshooting”For specific issues with directives, see the troubleshooting sections in:
Common issues
Section titled “Common issues”- Directive ignored: Check placement (must be first) and spelling
- Compilation still happens: Check
ignoreUseNoForgetsetting - Module directive not working: Ensure it’s before all imports
See also
Section titled “See also”compilationMode- Configure how the compiler chooses what to optimizeConfiguration- Full compiler configuration options- React Compiler documentation - Getting started guide