logo by @sawaratsuki1004
React
v19.2
Learn
Reference
Community
Blog

Is this page useful?

في هذه الصفحة

  • Overview
  • Reference
  • "use memo"
  • How "use memo" marks functions for optimization
  • When to use "use memo"
  • Usage
  • Working with different compilation modes
  • Troubleshooting
  • Verifying optimization
  • See also

    react@19.2

  • نظرة عامة
  • Hooks
    • useActionState
    • useCallback
    • useContext
    • useDebugValue
    • useDeferredValue
    • useEffect
    • useEffectEvent
    • useId
    • useImperativeHandle
    • useInsertionEffect
    • useLayoutEffect
    • useMemo
    • useOptimistic
    • useReducer
    • useRef
    • useState
    • useSyncExternalStore
    • useTransition
  • المكونات
    • <Fragment> (<>)
    • <Profiler>
    • <StrictMode>
    • <Suspense>
    • <Activity>
    • <ViewTransition> - This feature is available in the latest Canary version of React
  • APIs
    • act
    • addTransitionType - This feature is available in the latest Canary version of React
    • cache
    • cacheSignal
    • captureOwnerStack
    • createContext
    • lazy
    • memo
    • startTransition
    • use
    • experimental_taintObjectReference - This feature is available in the latest Experimental version of React
    • experimental_taintUniqueValue - This feature is available in the latest Experimental version of React
  • react-dom@19.2

  • Hooks
    • useFormStatus
  • المكونات (Components)
    • Common (e.g. <div>)
    • <form>
    • <input>
    • <option>
    • <progress>
    • <select>
    • <textarea>
    • <link>
    • <meta>
    • <script>
    • <style>
    • <title>
  • APIs
    • createPortal
    • flushSync
    • preconnect
    • prefetchDNS
    • preinit
    • preinitModule
    • preload
    • preloadModule
  • Client APIs
    • createRoot
    • hydrateRoot
  • Server APIs
    • renderToPipeableStream
    • renderToReadableStream
    • renderToStaticMarkup
    • renderToString
    • resume
    • resumeToPipeableStream
  • Static APIs
    • prerender
    • prerenderToNodeStream
    • resumeAndPrerender
    • resumeAndPrerenderToNodeStream
  • React Compiler

  • الإعدادات (Configuration)
    • compilationMode
    • gating
    • logger
    • panicThreshold
    • target
  • Directives
    • "use memo"
    • "use no memo"
  • تصريف المكتبات (Compiling Libraries)
  • React DevTools

  • React Performance tracks
  • eslint-plugin-react-hooks

  • Lints
    • exhaustive-deps
    • rules-of-hooks
    • component-hook-factories
    • config
    • error-boundaries
    • gating
    • globals
    • immutability
    • incompatible-library
    • preserve-manual-memoization
    • purity
    • refs
    • set-state-in-effect
    • set-state-in-render
    • static-components
    • unsupported-syntax
    • use-memo
  • قواعد React (Rules of React)

  • نظرة عامة (Overview)
    • Components و Hooks يجب أن تكون Pure
    • React تستدعي Components و Hooks
    • قواعد Hooks
  • React Server Components

  • Server Components
  • Server Functions
  • Directives
    • 'use client'
    • 'use server'
  • Legacy APIs

  • Legacy React APIs
    • Children
    • cloneElement
    • Component
    • createElement
    • createRef
    • forwardRef
    • isValidElement
    • PureComponent
مرجع API
Directives

use memo

"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
    • "use memo"
    • How "use memo" marks functions for optimization
    • When to use "use memo"
  • Usage
    • Working with different compilation modes
  • Troubleshooting
    • Verifying optimization
    • See also

Reference

"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

  • "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 compilationMode setting.

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 annotation mode: Only functions with "use memo" are optimized
  • In infer mode: The compiler uses heuristics, but "use memo" forces optimization
  • In all mode: 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"

You should consider using "use memo" when:

You’re using annotation mode

In compilationMode: 'annotation', the directive is required for any function you want optimized:

// ✅ This component will be optimized function OptimizedList() { "use memo"; // ... } // ❌ This component won't be optimized function SimpleWrapper() { // ... }

You’re gradually adopting React Compiler

Start with annotation mode and selectively optimize stable components:

// Start by optimizing leaf components function Button({ onClick, children }) { "use memo"; // ... } // Gradually move up the tree as you verify behavior function ButtonGroup({ buttons }) { "use memo"; // ... }


Usage

Working with different compilation modes

The behavior of "use memo" changes based on your compiler configuration:

// babel.config.js module.exports = { plugins: [ ['babel-plugin-react-compiler', { compilationMode: 'annotation' // or 'infer' or 'all' }] ] };

Annotation mode

// ✅ Optimized with "use memo" function ProductCard({ product }) { "use memo"; // ... } // ❌ Not optimized (no directive) function ProductList({ products }) { // ... }

Infer mode (default)

// Automatically memoized because this is named like a Component function ComplexDashboard({ data }) { // ... } // Skipped: Is not named like a Component function 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

Verifying optimization

To confirm your component is being optimized:

  1. Check the compiled output in your build
  2. Use React DevTools to check for Memo ✨ badge

See also

  • "use no memo" - Opt out of compilation
  • compilationMode - Configure compilation behavior
  • React Compiler - Getting started guide
السابقDirectives
التالي"use no memo"

Copyright © Meta Platforms, Inc
no uwu plz
uwu?
Logo by@sawaratsuki1004
تعلم React
بداية سريعة
التثبيت
وصف واجهة المستخدم (UI)
إضافة التفاعلية
إدارة State
مخارج الطوارئ
مرجع API
React APIs
React DOM APIs
المجتمع
ميثاق السلوك
تعرف على الفريق
المساهمون في التوثيق
شكر وتقدير
المزيد
المدونة
React Native
الخصوصية
الشروط
function MyComponent() {
"use memo";
// ...
}
// ✅ This component will be optimized
function OptimizedList() {
"use memo";
// ...
}

// ❌ This component won't be optimized
function SimpleWrapper() {
// ...
}
// Start by optimizing leaf components
function Button({ onClick, children }) {
"use memo";
// ...
}

// Gradually move up the tree as you verify behavior
function ButtonGroup({ buttons }) {
"use memo";
// ...
}
// babel.config.js
module.exports = {
plugins: [
['babel-plugin-react-compiler', {
compilationMode: 'annotation' // or 'infer' or 'all'
}]
]
};
// ✅ Optimized with "use memo"
function ProductCard({ product }) {
"use memo";
// ...
}

// ❌ Not optimized (no directive)
function ProductList({ products }) {
// ...
}
// Automatically memoized because this is named like a Component
function ComplexDashboard({ data }) {
// ...
}

// Skipped: Is not named like a Component
function simpleDisplay({ text }) {
// ...
}