Home / Function/ visitFunctionExpression() — react Function Reference

visitFunctionExpression() — react Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  450e9fa3_e9b3_e461_c1b5_503df5ad4c02["visitFunctionExpression()"]
  0aa1377a_e69b_36ae_b83d_c842baa2ad42["ValidateHooksUsage.ts"]
  450e9fa3_e9b3_e461_c1b5_503df5ad4c02 -->|defined in| 0aa1377a_e69b_36ae_b83d_c842baa2ad42
  6eadf343_46d9_d614_1b21_1060f09386fc["validateHooksUsage()"]
  6eadf343_46d9_d614_1b21_1060f09386fc -->|calls| 450e9fa3_e9b3_e461_c1b5_503df5ad4c02
  9cbd2355_05cd_5bbd_253f_7aeb4f947484["getHookKind()"]
  450e9fa3_e9b3_e461_c1b5_503df5ad4c02 -->|calls| 9cbd2355_05cd_5bbd_253f_7aeb4f947484
  825ba705_282a_7031_34fa_49b26f1c9bed["pushErrorDetail()"]
  450e9fa3_e9b3_e461_c1b5_503df5ad4c02 -->|calls| 825ba705_282a_7031_34fa_49b26f1c9bed
  style 450e9fa3_e9b3_e461_c1b5_503df5ad4c02 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateHooksUsage.ts lines 432–465

function visitFunctionExpression(errors: CompilerError, fn: HIRFunction): void {
  for (const [, block] of fn.body.blocks) {
    for (const instr of block.instructions) {
      switch (instr.value.kind) {
        case 'ObjectMethod':
        case 'FunctionExpression': {
          visitFunctionExpression(errors, instr.value.loweredFunc.func);
          break;
        }
        case 'MethodCall':
        case 'CallExpression': {
          const callee =
            instr.value.kind === 'CallExpression'
              ? instr.value.callee
              : instr.value.property;
          const hookKind = getHookKind(fn.env, callee.identifier);
          if (hookKind != null) {
            errors.pushErrorDetail(
              new CompilerErrorDetail({
                category: ErrorCategory.Hooks,
                reason:
                  'Hooks must be called at the top level in the body of a function component or custom hook, and may not be called within function expressions. See the Rules of Hooks (https://react.dev/warnings/invalid-hook-call-warning)',
                loc: callee.loc,
                description: `Cannot call ${hookKind === 'Custom' ? 'hook' : hookKind} within a function expression`,
                suggestions: null,
              }),
            );
          }
          break;
        }
      }
    }
  }
}

Domain

Subdomains

Frequently Asked Questions

What does visitFunctionExpression() do?
visitFunctionExpression() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateHooksUsage.ts.
Where is visitFunctionExpression() defined?
visitFunctionExpression() is defined in compiler/packages/babel-plugin-react-compiler/src/Validation/ValidateHooksUsage.ts at line 432.
What does visitFunctionExpression() call?
visitFunctionExpression() calls 2 function(s): getHookKind, pushErrorDetail.
What calls visitFunctionExpression()?
visitFunctionExpression() is called by 1 function(s): validateHooksUsage.

Analyze Your Own Codebase

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

Try Supermodel Free