Home / Function/ validateNoDynamicallyCreatedComponentsOrHooks() — react Function Reference

validateNoDynamicallyCreatedComponentsOrHooks() — react Function Reference

Architecture documentation for the validateNoDynamicallyCreatedComponentsOrHooks() function in Program.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  6f61e521_79f8_315c_2443_8700d7e01eaa["validateNoDynamicallyCreatedComponentsOrHooks()"]
  9aa4477d_960b_1ea1_b6d9_36076aaa70bd["Program.ts"]
  6f61e521_79f8_315c_2443_8700d7e01eaa -->|defined in| 9aa4477d_960b_1ea1_b6d9_36076aaa70bd
  7fe1be87_de24_09ca_229a_0644af0874a5["findFunctionsToCompile()"]
  7fe1be87_de24_09ca_229a_0644af0874a5 -->|calls| 6f61e521_79f8_315c_2443_8700d7e01eaa
  1351f300_d3f0_8ef1_7520_1bc143225b34["getFunctionName()"]
  6f61e521_79f8_315c_2443_8700d7e01eaa -->|calls| 1351f300_d3f0_8ef1_7520_1bc143225b34
  37c561cb_7b67_6332_ee68_b69aaf917134["getReactFunctionType()"]
  6f61e521_79f8_315c_2443_8700d7e01eaa -->|calls| 37c561cb_7b67_6332_ee68_b69aaf917134
  eaa25847_803a_a160_25ce_fa2699f69d1c["throwDiagnostic()"]
  6f61e521_79f8_315c_2443_8700d7e01eaa -->|calls| eaa25847_803a_a160_25ce_fa2699f69d1c
  style 6f61e521_79f8_315c_2443_8700d7e01eaa fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts lines 884–943

function validateNoDynamicallyCreatedComponentsOrHooks(
  fn: BabelFn,
  pass: CompilerPass,
  programContext: ProgramContext,
): void {
  const parentNameExpr = getFunctionName(fn);
  const parentName =
    parentNameExpr !== null && parentNameExpr.isIdentifier()
      ? parentNameExpr.node.name
      : '<anonymous>';

  const validateNestedFunction = (
    nestedFn: NodePath<
      t.FunctionDeclaration | t.FunctionExpression | t.ArrowFunctionExpression
    >,
  ): void => {
    if (
      nestedFn.node === fn.node ||
      programContext.alreadyCompiled.has(nestedFn.node)
    ) {
      return;
    }

    if (nestedFn.scope.getProgramParent() !== nestedFn.scope.parent) {
      const nestedFnType = getReactFunctionType(nestedFn as BabelFn, pass);
      const nestedFnNameExpr = getFunctionName(nestedFn as BabelFn);
      const nestedName =
        nestedFnNameExpr !== null && nestedFnNameExpr.isIdentifier()
          ? nestedFnNameExpr.node.name
          : '<anonymous>';
      if (nestedFnType === 'Component' || nestedFnType === 'Hook') {
        CompilerError.throwDiagnostic({
          category: ErrorCategory.Factories,
          reason: `Components and hooks cannot be created dynamically`,
          description: `The function \`${nestedName}\` appears to be a React ${nestedFnType.toLowerCase()}, but it's defined inside \`${parentName}\`. Components and Hooks should always be declared at module scope`,
          details: [
            {
              kind: 'error',
              message: 'this function dynamically created a component/hook',
              loc: parentNameExpr?.node.loc ?? fn.node.loc ?? null,
            },
            {
              kind: 'error',
              message: 'the component is created here',
              loc: nestedFnNameExpr?.node.loc ?? nestedFn.node.loc ?? null,
            },
          ],
        });
      }
    }

    nestedFn.skip();
  };

  fn.traverse({
    FunctionDeclaration: validateNestedFunction,
    FunctionExpression: validateNestedFunction,
    ArrowFunctionExpression: validateNestedFunction,
  });
}

Domain

Subdomains

Frequently Asked Questions

What does validateNoDynamicallyCreatedComponentsOrHooks() do?
validateNoDynamicallyCreatedComponentsOrHooks() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts.
Where is validateNoDynamicallyCreatedComponentsOrHooks() defined?
validateNoDynamicallyCreatedComponentsOrHooks() is defined in compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Program.ts at line 884.
What does validateNoDynamicallyCreatedComponentsOrHooks() call?
validateNoDynamicallyCreatedComponentsOrHooks() calls 3 function(s): getFunctionName, getReactFunctionType, throwDiagnostic.
What calls validateNoDynamicallyCreatedComponentsOrHooks()?
validateNoDynamicallyCreatedComponentsOrHooks() is called by 1 function(s): findFunctionsToCompile.

Analyze Your Own Codebase

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

Try Supermodel Free