ディレクティブ
React Compiler のディレクティブは、特定の関数をコンパイルするかどうかを制御する特別な文字列リテラルです。
function MyComponent() {
"use memo"; // Opt this component into compilation
return <div>{/* ... */}</div>;
}概要
React Compiler のディレクティブを使用することで、コンパイラがどの関数を最適化するかを細かく制御できます。ディレクティブは、関数本体の先頭またはモジュールの先頭に配置する文字列リテラルです。
利用可能なディレクティブ
"use memo"- 関数をコンパイル対象に含める"use no memo"- 関数をコンパイル対象から除外する
比較表
| ディレクティブ | 目的 | 使用する場面 |
|---|---|---|
"use memo" | コンパイルを強制 | annotation モードを使用する場合、または infer モードのヒューリスティックを上書きする場合 |
"use no memo" | コンパイルを防止 | 問題をデバッグする場合、または互換性のないコードを扱う場合 |
使用法
関数レベルのディレクティブ
関数のコンパイルを制御するには、その関数の先頭にディレクティブを配置します。
// 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>;
}コンパイルモードとの関係
ディレクティブの動作は、compilationMode によって異なります。
annotationモード:"use memo"がある関数のみをコンパイルするinferモード:コンパイラがコンパイル対象を決定し、ディレクティブがその決定を上書きするallモード:すべてをコンパイルし、"use no memo"で特定の関数を除外できる
ベストプラクティス
ディレクティブは必要な場合にのみ使用する
ディレクティブは避難ハッチです。プロジェクトレベルでコンパイラを設定することを優先してください。
// ✅ 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";
// ...
}削除する予定を立てておく
オプトアウト用のディレクティブは一時的なものにしてください。
- ディレクティブを追加する際は TODO コメント付きにする
- 追跡用の issue を作成する
- 大元にある問題を修正する
- ディレクティブを削除する
function TemporaryWorkaround() {
"use no memo"; // TODO: Remove after upgrading ThirdPartyLib to v2.0
return <ThirdPartyComponent />;
}よくあるパターン
段階的導入
大規模なコードベースに React Compiler を導入する場合は、次のようにします。
// 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
// ...
}トラブルシューティング
ディレクティブに関する具体的な問題については、以下のトラブルシューティングセクションを参照してください。
よくある問題
- ディレクティブが無視される:配置(先頭である必要があります)とスペルを確認する
- コンパイルが引き続き実行される:
ignoreUseNoForgetの設定を確認する - モジュールのディレクティブが動作しない:すべてのインポートより前にあることを確認する
関連項目
compilationMode- コンパイラが最適化対象を選択する方法を設定する- 設定 - コンパイラのすべての設定オプション
- React Compiler のドキュメント - 入門ガイド