Home / File/ lazyLegacyRoot.js — react Source File

lazyLegacyRoot.js — react Source File

Architecture documentation for lazyLegacyRoot.js, a javascript file in the react codebase. 4 imports, 1 dependents.

File javascript BabelCompiler Optimization 4 imports 1 dependents 2 functions

Entity Profile

Dependency Diagram

graph LR
  d273e762_42b4_589e_195c_7779bffc0b74["lazyLegacyRoot.js"]
  361ac9eb_af59_6b57_df64_dc69bf6f3c07["./shared/ThemeContext"]
  d273e762_42b4_589e_195c_7779bffc0b74 --> 361ac9eb_af59_6b57_df64_dc69bf6f3c07
  ac587885_e294_a1e9_b13f_5e7b920fdb42["react"]
  d273e762_42b4_589e_195c_7779bffc0b74 --> ac587885_e294_a1e9_b13f_5e7b920fdb42
  68ebf7af_04e4_49eb_c396_471f6297c18c["react-router"]
  d273e762_42b4_589e_195c_7779bffc0b74 --> 68ebf7af_04e4_49eb_c396_471f6297c18c
  56a19847_52ab_03d1_31e4_30711218e0ad["react-redux"]
  d273e762_42b4_589e_195c_7779bffc0b74 --> 56a19847_52ab_03d1_31e4_30711218e0ad
  367386fb_0810_3989_407d_4f7672d39325["AboutPage.js"]
  367386fb_0810_3989_407d_4f7672d39325 --> d273e762_42b4_589e_195c_7779bffc0b74
  style d273e762_42b4_589e_195c_7779bffc0b74 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import React from 'react';
import {useContext, useMemo, useRef, useState, useLayoutEffect} from 'react';
import {__RouterContext} from 'react-router';
import {ReactReduxContext} from 'react-redux';

import ThemeContext from './shared/ThemeContext';

let rendererModule = {
  status: 'pending',
  promise: null,
  result: null,
};

export default function lazyLegacyRoot(getLegacyComponent) {
  let componentModule = {
    status: 'pending',
    promise: null,
    result: null,
  };

  return function Wrapper(props) {
    const createLegacyRoot = readModule(
      rendererModule,
      () => import('../legacy/createLegacyRoot')
    ).default;
    const Component = readModule(componentModule, getLegacyComponent).default;
    const containerRef = useRef(null);
    const rootRef = useRef(null);

    // Populate every contexts we want the legacy subtree to see.
    // Then in src/legacy/createLegacyRoot we will apply them.
    const theme = useContext(ThemeContext);
    const router = useContext(__RouterContext);
    const reactRedux = useContext(ReactReduxContext);
    const context = useMemo(
      () => ({
        theme,
        router,
        reactRedux,
      }),
      [theme, router, reactRedux]
    );

    // Create/unmount.
    useLayoutEffect(() => {
      if (!rootRef.current) {
        rootRef.current = createLegacyRoot(containerRef.current);
      }
      const root = rootRef.current;
      return () => {
        root.unmount();
      };
    }, [createLegacyRoot]);

    // Mount/update.
    useLayoutEffect(() => {
      if (rootRef.current) {
        rootRef.current.render(Component, props, context);
      }
    }, [Component, props, context]);

    return <div style={{display: 'contents'}} ref={containerRef} />;
  };
}

// This is similar to React.lazy, but implemented manually.
// We use this to Suspend rendering of this component until
// we fetch the component and the legacy React to render it.
function readModule(record, createPromise) {
  if (record.status === 'fulfilled') {
    return record.result;
  }
  if (record.status === 'rejected') {
    throw record.result;
  }
  if (!record.promise) {
    record.promise = createPromise().then(
      value => {
        if (record.status === 'pending') {
          record.status = 'fulfilled';
          record.promise = null;
          record.result = value;
        }
      },
      error => {
        if (record.status === 'pending') {
          record.status = 'rejected';
          record.promise = null;
          record.result = error;
        }
      }
    );
  }
  throw record.promise;
}

Domain

Subdomains

Dependencies

  • ./shared/ThemeContext
  • react
  • react-redux
  • react-router

Frequently Asked Questions

What does lazyLegacyRoot.js do?
lazyLegacyRoot.js is a source file in the react codebase, written in javascript. It belongs to the BabelCompiler domain, Optimization subdomain.
What functions are defined in lazyLegacyRoot.js?
lazyLegacyRoot.js defines 2 function(s): lazyLegacyRoot, readModule.
What does lazyLegacyRoot.js depend on?
lazyLegacyRoot.js imports 4 module(s): ./shared/ThemeContext, react, react-redux, react-router.
What files import lazyLegacyRoot.js?
lazyLegacyRoot.js is imported by 1 file(s): AboutPage.js.
Where is lazyLegacyRoot.js in the architecture?
lazyLegacyRoot.js is located at fixtures/nesting/src/modern/lazyLegacyRoot.js (domain: BabelCompiler, subdomain: Optimization, directory: fixtures/nesting/src/modern).

Analyze Your Own Codebase

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

Try Supermodel Free