Home / Function/ attach() — react Function Reference

attach() — react Function Reference

Architecture documentation for the attach() function in renderer.js from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  5bd02f8e_960a_28df_cd57_8c68e5d3f04f["attach()"]
  50ab7512_20ae_29b1_e876_3d07fe8f521c["renderer.js"]
  5bd02f8e_960a_28df_cd57_8c68e5d3f04f -->|defined in| 50ab7512_20ae_29b1_e876_3d07fe8f521c
  458d9ad5_5fd5_f1a4_da0b_de62b2eabee3["supportsConsoleTasks()"]
  5bd02f8e_960a_28df_cd57_8c68e5d3f04f -->|calls| 458d9ad5_5fd5_f1a4_da0b_de62b2eabee3
  a8f4c31f_f504_975a_5ac7_bb83d1cf4164["formatOwnerStack()"]
  5bd02f8e_960a_28df_cd57_8c68e5d3f04f -->|calls| a8f4c31f_f504_975a_5ac7_bb83d1cf4164
  0d0f5eb3_f682_c269_a97f_22be22717aa0["getOwnerStackByComponentInfoInDev()"]
  5bd02f8e_960a_28df_cd57_8c68e5d3f04f -->|calls| 0d0f5eb3_f682_c269_a97f_22be22717aa0
  style 5bd02f8e_960a_28df_cd57_8c68e5d3f04f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/react-devtools-shared/src/backend/flight/renderer.js lines 29–206

export function attach(
  hook: DevToolsHook,
  rendererID: number,
  renderer: ReactRenderer,
  global: Object,
): RendererInterface {
  const {getCurrentComponentInfo} = renderer;

  function getComponentStack(
    topFrame: Error,
  ): null | {enableOwnerStacks: boolean, componentStack: string} {
    if (getCurrentComponentInfo === undefined) {
      // Expected this to be part of the renderer. Ignore.
      return null;
    }
    const current = getCurrentComponentInfo();
    if (current === null) {
      // Outside of our render scope.
      return null;
    }

    if (supportsConsoleTasks(current)) {
      // This will be handled natively by console.createTask. No need for
      // DevTools to add it.
      return null;
    }

    const enableOwnerStacks = current.debugStack != null;
    let componentStack = '';
    if (enableOwnerStacks) {
      // Prefix the owner stack with the current stack. I.e. what called
      // console.error. While this will also be part of the native stack,
      // it is hidden and not presented alongside this argument so we print
      // them all together.
      const topStackFrames = formatOwnerStack(topFrame);
      if (topStackFrames) {
        componentStack += '\n' + topStackFrames;
      }
      componentStack += getOwnerStackByComponentInfoInDev(current);
    }
    return {enableOwnerStacks, componentStack};
  }

  // Called when an error or warning is logged during render, commit, or passive (including unmount functions).
  function onErrorOrWarning(
    type: 'error' | 'warn',
    args: $ReadOnlyArray<any>,
  ): void {
    if (getCurrentComponentInfo === undefined) {
      // Expected this to be part of the renderer. Ignore.
      return;
    }
    const componentInfo = getCurrentComponentInfo();
    if (componentInfo === null) {
      // Outside of our render scope.
      return;
    }

    if (
      args.length > 3 &&
      typeof args[0] === 'string' &&
      args[0].startsWith('%c%s%c ') &&
      typeof args[1] === 'string' &&
      typeof args[2] === 'string' &&
      typeof args[3] === 'string'
    ) {
      // This looks like the badge we prefixed to the log. Our UI doesn't support formatted logs.
      // We remove the formatting. If the environment of the log is the same as the environment of
      // the component (the common case) we remove the badge completely otherwise leave it plain
      const format = args[0].slice(7);
      const env = args[2].trim();
      args = args.slice(4);
      if (env !== componentInfo.env) {
        args.unshift('[' + env + '] ' + format);
      } else {
        args.unshift(format);
      }
    }

    // We can't really use this message as a unique key, since we can't distinguish
    // different objects in this implementation. We have to delegate displaying of the objects

Domain

Subdomains

Frequently Asked Questions

What does attach() do?
attach() is a function in the react codebase, defined in packages/react-devtools-shared/src/backend/flight/renderer.js.
Where is attach() defined?
attach() is defined in packages/react-devtools-shared/src/backend/flight/renderer.js at line 29.
What does attach() call?
attach() calls 3 function(s): formatOwnerStack, getOwnerStackByComponentInfoInDev, supportsConsoleTasks.

Analyze Your Own Codebase

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

Try Supermodel Free