Home / Function/ validateHooksUsage() — react Function Reference

validateHooksUsage() — react Function Reference

Architecture documentation for the validateHooksUsage() function in ValidateHooksUsage.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  6eadf343_46d9_d614_1b21_1060f09386fc["validateHooksUsage()"]
  0aa1377a_e69b_36ae_b83d_c842baa2ad42["ValidateHooksUsage.ts"]
  6eadf343_46d9_d614_1b21_1060f09386fc -->|defined in| 0aa1377a_e69b_36ae_b83d_c842baa2ad42
  cc92da6a_36c2_147e_624d_3b9a7d1999b0["computeUnconditionalBlocks()"]
  6eadf343_46d9_d614_1b21_1060f09386fc -->|calls| cc92da6a_36c2_147e_624d_3b9a7d1999b0
  825ba705_282a_7031_34fa_49b26f1c9bed["pushErrorDetail()"]
  6eadf343_46d9_d614_1b21_1060f09386fc -->|calls| 825ba705_282a_7031_34fa_49b26f1c9bed
  c32c4801_8f1e_6ab5_ff15_ac8ec7df6945["isHookName()"]
  6eadf343_46d9_d614_1b21_1060f09386fc -->|calls| c32c4801_8f1e_6ab5_ff15_ac8ec7df6945
  9652f5cc_e476_cf2b_aa60_27362aa0b748["joinKinds()"]
  6eadf343_46d9_d614_1b21_1060f09386fc -->|calls| 9652f5cc_e476_cf2b_aa60_27362aa0b748
  9cbd2355_05cd_5bbd_253f_7aeb4f947484["getHookKind()"]
  6eadf343_46d9_d614_1b21_1060f09386fc -->|calls| 9cbd2355_05cd_5bbd_253f_7aeb4f947484
  d7fde76c_4fd9_feb3_299b_798689f05bc6["assertExhaustive()"]
  6eadf343_46d9_d614_1b21_1060f09386fc -->|calls| d7fde76c_4fd9_feb3_299b_798689f05bc6
  ccace1c3_85b7_a05e_c2a5_7eff8b3422ed["eachInstructionOperand()"]
  6eadf343_46d9_d614_1b21_1060f09386fc -->|calls| ccace1c3_85b7_a05e_c2a5_7eff8b3422ed
  10043bf1_f7ee_9ed9_307a_fe3edfd02b09["eachInstructionLValue()"]
  6eadf343_46d9_d614_1b21_1060f09386fc -->|calls| 10043bf1_f7ee_9ed9_307a_fe3edfd02b09
  450e9fa3_e9b3_e461_c1b5_503df5ad4c02["visitFunctionExpression()"]
  6eadf343_46d9_d614_1b21_1060f09386fc -->|calls| 450e9fa3_e9b3_e461_c1b5_503df5ad4c02
  41232a25_deb6_6e83_05a8_ae9f961656f7["eachTerminalOperand()"]
  6eadf343_46d9_d614_1b21_1060f09386fc -->|calls| 41232a25_deb6_6e83_05a8_ae9f961656f7
  531eb985_e192_f9a2_2d7b_5deeb85ba95c["asResult()"]
  6eadf343_46d9_d614_1b21_1060f09386fc -->|calls| 531eb985_e192_f9a2_2d7b_5deeb85ba95c
  style 6eadf343_46d9_d614_1b21_1060f09386fc fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateHooksUsage.ts lines 91–430

export function validateHooksUsage(
  fn: HIRFunction,
): Result<void, CompilerError> {
  const unconditionalBlocks = computeUnconditionalBlocks(fn);

  const errors = new CompilerError();
  const errorsByPlace = new Map<t.SourceLocation, CompilerErrorDetail>();

  function recordError(
    loc: SourceLocation,
    errorDetail: CompilerErrorDetail,
  ): void {
    if (typeof loc === 'symbol') {
      errors.pushErrorDetail(errorDetail);
    } else {
      errorsByPlace.set(loc, errorDetail);
    }
  }

  function recordConditionalHookError(place: Place): void {
    // Once a particular hook has a conditional call error, don't report any further issues for this hook
    setKind(place, Kind.Error);

    const reason =
      'Hooks must always be called in a consistent order, and may not be called conditionally. See the Rules of Hooks (https://react.dev/warnings/invalid-hook-call-warning)';
    const previousError =
      typeof place.loc !== 'symbol' ? errorsByPlace.get(place.loc) : undefined;

    /*
     * In some circumstances such as optional calls, we may first encounter a "hook may not be referenced as normal values" error.
     * If that same place is also used as a conditional call, upgrade the error to a conditonal hook error
     */
    if (previousError === undefined || previousError.reason !== reason) {
      recordError(
        place.loc,
        new CompilerErrorDetail({
          category: ErrorCategory.Hooks,
          description: null,
          reason,
          loc: place.loc,
          suggestions: null,
        }),
      );
    }
  }
  function recordInvalidHookUsageError(place: Place): void {
    const previousError =
      typeof place.loc !== 'symbol' ? errorsByPlace.get(place.loc) : undefined;
    if (previousError === undefined) {
      recordError(
        place.loc,
        new CompilerErrorDetail({
          category: ErrorCategory.Hooks,
          description: null,
          reason:
            'Hooks may not be referenced as normal values, they must be called. See https://react.dev/reference/rules/react-calls-components-and-hooks#never-pass-around-hooks-as-regular-values',
          loc: place.loc,
          suggestions: null,
        }),
      );
    }
  }
  function recordDynamicHookUsageError(place: Place): void {
    const previousError =
      typeof place.loc !== 'symbol' ? errorsByPlace.get(place.loc) : undefined;
    if (previousError === undefined) {
      recordError(
        place.loc,
        new CompilerErrorDetail({
          category: ErrorCategory.Hooks,
          description: null,
          reason:
            'Hooks must be the same function on every render, but this value may change over time to a different function. See https://react.dev/reference/rules/react-calls-components-and-hooks#dont-dynamically-use-hooks',
          loc: place.loc,
          suggestions: null,
        }),
      );
    }
  }

  const valueKinds = new Map<IdentifierId, Kind>();

Domain

Subdomains

Frequently Asked Questions

What does validateHooksUsage() do?
validateHooksUsage() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateHooksUsage.ts.
Where is validateHooksUsage() defined?
validateHooksUsage() is defined in compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateHooksUsage.ts at line 91.
What does validateHooksUsage() call?
validateHooksUsage() calls 11 function(s): asResult, assertExhaustive, computeUnconditionalBlocks, eachInstructionLValue, eachInstructionOperand, eachTerminalOperand, getHookKind, isHookName, and 3 more.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free