logo by @sawaratsuki1004
React
v19.2
تعلم
مرجع
المجتمع
المدونة

هل هذه الصفحة مفيدة؟

في هذه الصفحة

  • Overview
  • Overview
  • Available directives
  • Quick comparison
  • Usage
  • Function-level directives
  • Module-level directives
  • Compilation modes interaction
  • Best practices
  • Use directives sparingly
  • Document directive usage
  • Plan for removal
  • Common patterns
  • Gradual adoption
  • Troubleshooting
  • Common issues
  • 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> - هذه الميزة متاحة في أحدث إصدار Canary من React
  • APIs
    • act
    • addTransitionType - هذه الميزة متاحة في أحدث إصدار Canary من React
    • cache
    • cacheSignal
    • captureOwnerStack
    • createContext
    • lazy
    • memo
    • startTransition
    • use
    • experimental_taintObjectReference - هذه الميزة متاحة في أحدث إصدار تجريبي من React
    • experimental_taintUniqueValue - هذه الميزة متاحة في أحدث إصدار تجريبي من 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

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
    • Available directives
    • Quick comparison
  • Usage
    • Function-level directives
    • Module-level directives
    • Compilation modes interaction
  • Best practices
    • Use directives sparingly
    • Document directive usage
    • Plan for removal
  • Common patterns
    • Gradual adoption
  • Troubleshooting
    • Common issues
  • See also

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

  • "use memo" - Opts a function into compilation
  • "use no memo" - Opts a function out of compilation

Quick comparison

DirectivePurposeWhen to use
"use memo"Force compilationWhen using annotation mode or to override infer mode heuristics
"use no memo"Prevent compilationDebugging issues or working with incompatible code

Usage

Function-level directives

Place directives at the beginning of a function to control its compilation:

// Opt into compilation function OptimizedComponent() { "use memo"; return <div>This will be optimized</div>; } // Opt out of compilation function UnoptimizedComponent() { "use no memo"; return <div>This won't be optimized</div>; }

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 compiled function Component1() { return <div>Compiled</div>; } function Component2() { return <div>Also compiled</div>; } // Can be overridden at function level function Component3() { "use no memo"; // This overrides the module directive return <div>Not compiled</div>; }

Compilation modes interaction

Directives behave differently depending on your compilationMode:

  • annotation mode: Only functions with "use memo" are compiled
  • infer mode: Compiler decides what to compile, directives override decisions
  • all mode: Everything is compiled, "use no memo" can exclude specific functions

Best practices

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 needed function SpecialCase() { "use no memo"; // Document why this is needed // ... }

Document directive usage

Always explain why a directive is used:

// ✅ Good - clear explanation function DataGrid() { "use no memo"; // TODO: Remove after fixing issue with dynamic row heights (JIRA-123) // Complex grid implementation } // ❌ Bad - no explanation function Mystery() { "use no memo"; // ... }

Plan for removal

Opt-out directives should be temporary:

  1. Add the directive with a TODO comment
  2. Create a tracking issue
  3. Fix the underlying problem
  4. Remove the directive

function TemporaryWorkaround() { "use no memo"; // TODO: Remove after upgrading ThirdPartyLib to v2.0 return <ThirdPartyComponent />; }


Common patterns

Gradual adoption

When adopting the React Compiler in a large codebase:

// Start with annotation mode { compilationMode: 'annotation' } // Opt in stable components function StableComponent() { "use memo"; // Well-tested component } // Later, switch to infer mode and opt out problematic ones function ProblematicComponent() { "use no memo"; // Fix issues before removing // ... }


Troubleshooting

For specific issues with directives, see the troubleshooting sections in:

  • "use memo" troubleshooting
  • "use no memo" troubleshooting

Common issues

  1. Directive ignored: Check placement (must be first) and spelling
  2. Compilation still happens: Check ignoreUseNoForget setting
  3. Module directive not working: Ensure it’s before all imports

See also

  • compilationMode - Configure how the compiler chooses what to optimize
  • Configuration - Full compiler configuration options
  • React Compiler documentation - Getting started guide
السابقtarget
التالي"use 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"; // Opt this component into compilation
return <div>{/* ... */}</div>;
}
// Opt into compilation
function OptimizedComponent() {
"use memo";
return <div>This will be optimized</div>;
}

// Opt out of compilation
function UnoptimizedComponent() {
"use no memo";
return <div>This won't be optimized</div>;
}
// At the very top of the file
"use memo";

// All functions in this file will be compiled
function Component1() {
return <div>Compiled</div>;
}

function Component2() {
return <div>Also compiled</div>;
}

// Can be overridden at function level
function Component3() {
"use no memo"; // This overrides the module directive
return <div>Not compiled</div>;
}
// ✅ Good - project-wide configuration
{
plugins: [
['babel-plugin-react-compiler', {
compilationMode: 'infer'
}]
]
}

// ⚠️ Use directives only when needed
function SpecialCase() {
"use no memo"; // Document why this is needed
// ...
}
// ✅ Good - clear explanation
function DataGrid() {
"use no memo"; // TODO: Remove after fixing issue with dynamic row heights (JIRA-123)
// Complex grid implementation
}

// ❌ Bad - no explanation
function Mystery() {
"use no memo";
// ...
}
function TemporaryWorkaround() {
"use no memo"; // TODO: Remove after upgrading ThirdPartyLib to v2.0
return <ThirdPartyComponent />;
}
// Start with annotation mode
{
compilationMode: 'annotation'
}

// Opt in stable components
function StableComponent() {
"use memo";
// Well-tested component
}

// Later, switch to infer mode and opt out problematic ones
function ProblematicComponent() {
"use no memo"; // Fix issues before removing
// ...
}