Home / Function/ $dispatcherGuard() — react Function Reference

$dispatcherGuard() — react Function Reference

Architecture documentation for the $dispatcherGuard() function in index.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  89475c27_36e1_b642_e8a4_3f8ac66c9bff["$dispatcherGuard()"]
  e7e94027_87c5_661b_dd97_be583c709eb3["index.ts"]
  89475c27_36e1_b642_e8a4_3f8ac66c9bff -->|defined in| e7e94027_87c5_661b_dd97_be583c709eb3
  db3e33e1_21b7_d6bd_c6f4_9aa335a423ab["setCurrent()"]
  89475c27_36e1_b642_e8a4_3f8ac66c9bff -->|calls| db3e33e1_21b7_d6bd_c6f4_9aa335a423ab
  style 89475c27_36e1_b642_e8a4_3f8ac66c9bff fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/react-compiler-runtime/src/index.ts lines 136–184

export function $dispatcherGuard(kind: GuardKind) {
  const curr = ReactSecretInternals.ReactCurrentDispatcher.current;
  if (kind === GuardKind.PushGuardContext) {
    // Push before checking invariant or errors
    guardFrames.push(curr);

    if (guardFrames.length === 1) {
      // save if we're the first guard on the stack
      originalDispatcher = curr;
    }

    if (curr === LazyGuardDispatcher) {
      throw new Error(
        `[React] Unexpected call to custom hook or component from a React compiled function. ` +
          "Check that (1) all hooks are called directly and named according to convention ('use[A-Z]') " +
          'and (2) components are returned as JSX instead of being directly invoked.',
      );
    }
    setCurrent(LazyGuardDispatcher);
  } else if (kind === GuardKind.PopGuardContext) {
    // Pop before checking invariant or errors
    const lastFrame = guardFrames.pop();

    if (lastFrame == null) {
      throw new Error(
        'React Compiler internal error: unexpected null in guard stack',
      );
    }
    if (guardFrames.length === 0) {
      originalDispatcher = null;
    }
    setCurrent(lastFrame);
  } else if (kind === GuardKind.PushExpectHook) {
    // ExpectHooks could be nested, so we save the current dispatcher
    // for the matching PopExpectHook to restore.
    guardFrames.push(curr);
    setCurrent(originalDispatcher);
  } else if (kind === GuardKind.PopExpectHook) {
    const lastFrame = guardFrames.pop();
    if (lastFrame == null) {
      throw new Error(
        'React Compiler internal error: unexpected null in guard stack',
      );
    }
    setCurrent(lastFrame);
  } else {
    throw new Error('React Compiler internal error: unreachable block' + kind);
  }
}

Domain

Subdomains

Calls

Frequently Asked Questions

What does $dispatcherGuard() do?
$dispatcherGuard() is a function in the react codebase, defined in compiler/packages/react-compiler-runtime/src/index.ts.
Where is $dispatcherGuard() defined?
$dispatcherGuard() is defined in compiler/packages/react-compiler-runtime/src/index.ts at line 136.
What does $dispatcherGuard() call?
$dispatcherGuard() calls 1 function(s): setCurrent.

Analyze Your Own Codebase

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

Try Supermodel Free