Home / File/ context.js — react Source File

context.js — react Source File

Architecture documentation for context.js, a javascript file in the react codebase. 8 imports, 2 dependents.

File javascript BabelCompiler 8 imports 2 dependents

Entity Profile

Dependency Diagram

graph LR
  b8ce5a14_1cf8_1044_56d2_6d19fb6e7b7b["context.js"]
  b9d81c20_b199_0aa9_b43a_53a3f46ca053["types.js"]
  b8ce5a14_1cf8_1044_56d2_6d19fb6e7b7b --> b9d81c20_b199_0aa9_b43a_53a3f46ca053
  d8f20c67_f5fa_0f0a_c967_c41fd9ffce07["ReactTypes"]
  b8ce5a14_1cf8_1044_56d2_6d19fb6e7b7b --> d8f20c67_f5fa_0f0a_c967_c41fd9ffce07
  ac587885_e294_a1e9_b13f_5e7b920fdb42["react"]
  b8ce5a14_1cf8_1044_56d2_6d19fb6e7b7b --> ac587885_e294_a1e9_b13f_5e7b920fdb42
  7cd456b6_507e_98de_1e07_f65fc1623ee5["context"]
  b8ce5a14_1cf8_1044_56d2_6d19fb6e7b7b --> 7cd456b6_507e_98de_1e07_f65fc1623ee5
  a185f695_5af9_82f0_a43a_ac143541224e["TreeContext"]
  b8ce5a14_1cf8_1044_56d2_6d19fb6e7b7b --> a185f695_5af9_82f0_a43a_ac143541224e
  4077b620_5c59_61c2_0910_273f565da757["bridge"]
  b8ce5a14_1cf8_1044_56d2_6d19fb6e7b7b --> 4077b620_5c59_61c2_0910_273f565da757
  baf70b0e_4867_b3ed_962a_e5c9ae820fef["store"]
  b8ce5a14_1cf8_1044_56d2_6d19fb6e7b7b --> baf70b0e_4867_b3ed_962a_e5c9ae820fef
  5fdaa481_4541_9cb7_b7d3_998d81934aa0["types"]
  b8ce5a14_1cf8_1044_56d2_6d19fb6e7b7b --> 5fdaa481_4541_9cb7_b7d3_998d81934aa0
  a2fac529_5caa_fd15_61d0_e5b11d75bdd2["Components.js"]
  a2fac529_5caa_fd15_61d0_e5b11d75bdd2 --> b8ce5a14_1cf8_1044_56d2_6d19fb6e7b7b
  a9cbaa5a_1a08_c395_56b8_61ce5609ff01["index.js"]
  a9cbaa5a_1a08_c395_56b8_61ce5609ff01 --> b8ce5a14_1cf8_1044_56d2_6d19fb6e7b7b
  style b8ce5a14_1cf8_1044_56d2_6d19fb6e7b7b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @flow
 */

import type {ReactContext} from 'shared/ReactTypes';

import * as React from 'react';
import {createContext, useContext, useEffect, useState} from 'react';
import {
  BridgeContext,
  StoreContext,
} from 'react-devtools-shared/src/devtools/views/context';
import {TreeStateContext} from 'react-devtools-shared/src/devtools/views/Components/TreeContext';

import type {StateContext} from 'react-devtools-shared/src/devtools/views/Components/TreeContext';
import type {FrontendBridge} from 'react-devtools-shared/src/bridge';
import type Store from 'react-devtools-shared/src/devtools/store';
import type {StyleAndLayout as StyleAndLayoutBackend} from 'react-devtools-shared/src/backend/NativeStyleEditor/types';
import type {StyleAndLayout as StyleAndLayoutFrontend} from './types';

type Context = StyleAndLayoutFrontend | null;

const NativeStyleContext: ReactContext<Context> = createContext<Context>(
  ((null: any): Context),
);
NativeStyleContext.displayName = 'NativeStyleContext';

type Props = {
  children: React$Node,
};

function NativeStyleContextController({children}: Props): React.Node {
  const bridge = useContext<FrontendBridge>(BridgeContext);
  const store = useContext<Store>(StoreContext);
  const {inspectedElementID} = useContext<StateContext>(TreeStateContext);

  const [currentStyleAndLayout, setCurrentStyleAndLayout] =
    useState<StyleAndLayoutFrontend | null>(null);

  // This effect handler polls for updates on the currently selected element.
  useEffect(() => {
    if (inspectedElementID === null) {
      setCurrentStyleAndLayout(null);
      return () => {};
    }

    let requestTimeoutId: TimeoutID | null = null;
    const sendRequest = () => {
      requestTimeoutId = null;
      const rendererID = store.getRendererIDForElement(inspectedElementID);

      if (rendererID !== null) {
        bridge.send('NativeStyleEditor_measure', {
          id: inspectedElementID,
          rendererID,
        });
      }
    };

    // Send the initial measurement request.
    // We'll poll for an update in the response handler below.
    sendRequest();

    const onStyleAndLayout = ({id, layout, style}: StyleAndLayoutBackend) => {
      // If this is the element we requested, wait a little bit and then ask for another update.
      if (id === inspectedElementID) {
        if (requestTimeoutId !== null) {
          clearTimeout(requestTimeoutId);
        }
        requestTimeoutId = setTimeout(sendRequest, 1000);
      }

      const styleAndLayout: StyleAndLayoutFrontend = {
        layout,
        style,
      };
      setCurrentStyleAndLayout(styleAndLayout);
    };

    bridge.addListener('NativeStyleEditor_styleAndLayout', onStyleAndLayout);
    return () => {
      bridge.removeListener(
        'NativeStyleEditor_styleAndLayout',
        onStyleAndLayout,
      );

      if (requestTimeoutId !== null) {
        clearTimeout(requestTimeoutId);
      }
    };
  }, [bridge, inspectedElementID, store]);

  return (
    <NativeStyleContext.Provider value={currentStyleAndLayout}>
      {children}
    </NativeStyleContext.Provider>
  );
}

export {NativeStyleContext, NativeStyleContextController};

Domain

Dependencies

  • ReactTypes
  • TreeContext
  • bridge
  • context
  • react
  • store
  • types
  • types.js

Frequently Asked Questions

What does context.js do?
context.js is a source file in the react codebase, written in javascript. It belongs to the BabelCompiler domain.
What does context.js depend on?
context.js imports 8 module(s): ReactTypes, TreeContext, bridge, context, react, store, types, types.js.
What files import context.js?
context.js is imported by 2 file(s): Components.js, index.js.
Where is context.js in the architecture?
context.js is located at packages/react-devtools-shared/src/devtools/views/Components/NativeStyleEditor/context.js (domain: BabelCompiler, directory: packages/react-devtools-shared/src/devtools/views/Components/NativeStyleEditor).

Analyze Your Own Codebase

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

Try Supermodel Free