"use memo" marks a function for React Compiler optimization.
In most cases, you don’t need "use memo". It’s primarily needed in annotation mode where you must explicitly mark functions for optimization. In infer mode, the compiler automatically detects components and hooks by their naming patterns (PascalCase for components, use prefix for hooks). If a component or hook isn’t being compiled in infer mode, you should fix its naming convention rather than forcing compilation with "use memo".
Reference
Section titled “Reference”"use memo"
Section titled “"use memo"”Add "use memo" at the beginning of a function to mark it for React Compiler optimization.
function MyComponent() { "use memo"; // ...}When a function contains "use memo", the React Compiler will analyze and optimize it during build time. The compiler will automatically memoize values and components to prevent unnecessary re-computations and re-renders.
Caveats
Section titled “Caveats”"use memo"must be at the very beginning of a function body, before any imports or other code (comments are OK).- The directive must be written with double or single quotes, not backticks.
- The directive must exactly match
"use memo". - Only the first directive in a function is processed; additional directives are ignored.
- The effect of the directive depends on your
compilationModesetting.
How "use memo" marks functions for optimization
Section titled “How "use memo" marks functions for optimization”In a React app that uses the React Compiler, functions are analyzed at build time to determine if they can be optimized. By default, the compiler automatically infers which components to memoize, but this can depend on your compilationMode setting if you’ve set it.
"use memo" explicitly marks a function for optimization, overriding the default behavior:
- In
annotationmode: Only functions with"use memo"are optimized - In
infermode: The compiler uses heuristics, but"use memo"forces optimization - In
allmode: Everything is optimized by default, making"use memo"redundant
The directive creates a clear boundary in your codebase between optimized and non-optimized code, giving you fine-grained control over the compilation process.
When to use "use memo"
Section titled “When to use "use memo"”You should consider using "use memo" when:
You’re using annotation mode
Section titled “You’re using annotation mode”In compilationMode: 'annotation', the directive is required for any function you want optimized:
// ✅ This component will be optimizedfunction OptimizedList() { "use memo"; // ...}
// ❌ This component won't be optimizedfunction SimpleWrapper() { // ...}You’re gradually adopting React Compiler
Section titled “You’re gradually adopting React Compiler”Start with annotation mode and selectively optimize stable components:
// Start by optimizing leaf componentsfunction Button({ onClick, children }) { "use memo"; // ...}
// Gradually move up the tree as you verify behaviorfunction ButtonGroup({ buttons }) { "use memo"; // ...}Working with different compilation modes
Section titled “Working with different compilation modes”The behavior of "use memo" changes based on your compiler configuration:
module.exports = { plugins: [ ['babel-plugin-react-compiler', { compilationMode: 'annotation' // or 'infer' or 'all' }] ]};Annotation mode
Section titled “Annotation mode”// ✅ Optimized with "use memo"function ProductCard({ product }) { "use memo"; // ...}
// ❌ Not optimized (no directive)function ProductList({ products }) { // ...}Infer mode (default)
Section titled “Infer mode (default)”// Automatically memoized because this is named like a Componentfunction ComplexDashboard({ data }) { // ...}
// Skipped: Is not named like a Componentfunction simpleDisplay({ text }) { // ...}In infer mode, the compiler automatically detects components and hooks by their naming patterns (PascalCase for components, use prefix for hooks). If a component or hook isn’t being compiled in infer mode, you should fix its naming convention rather than forcing compilation with "use memo".
Troubleshooting
Section titled “Troubleshooting”Verifying optimization
Section titled “Verifying optimization”To confirm your component is being optimized:
- Check the compiled output in your build
- Use React DevTools to check for Memo ✨ badge
See also
Section titled “See also”"use no memo"- Opt out of compilationcompilationMode- Configure compilation behavior- React Compiler - Getting started guide