Home / Function/ mountClassInstance() — react Function Reference

mountClassInstance() — react Function Reference

Architecture documentation for the mountClassInstance() function in ReactFiberClassComponent.js from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  3d21bb44_e12a_666f_ff91_4155c0f8d71b["mountClassInstance()"]
  3805476a_1924_0e35_fff7_6afad197a523["ReactFiberClassComponent.js"]
  3d21bb44_e12a_666f_ff91_4155c0f8d71b -->|defined in| 3805476a_1924_0e35_fff7_6afad197a523
  9bea19b8_e307_63f1_e3b4_89c20a3fd20b["updateClassComponent()"]
  9bea19b8_e307_63f1_e3b4_89c20a3fd20b -->|calls| 3d21bb44_e12a_666f_ff91_4155c0f8d71b
  0516fb13_8886_d15c_4bfa_1bd5d35db45d["mountIncompleteClassComponent()"]
  0516fb13_8886_d15c_4bfa_1bd5d35db45d -->|calls| 3d21bb44_e12a_666f_ff91_4155c0f8d71b
  3e3ed3f4_92fe_b808_59f1_ca6499a18813["checkClassInstance()"]
  3d21bb44_e12a_666f_ff91_4155c0f8d71b -->|calls| 3e3ed3f4_92fe_b808_59f1_ca6499a18813
  9f4500d1_a30b_b3dc_2405_2ab2117c51e4["applyDerivedStateFromProps()"]
  3d21bb44_e12a_666f_ff91_4155c0f8d71b -->|calls| 9f4500d1_a30b_b3dc_2405_2ab2117c51e4
  9d2b4de0_7d00_8afe_d25a_63046c5f7d5d["callComponentWillMount()"]
  3d21bb44_e12a_666f_ff91_4155c0f8d71b -->|calls| 9d2b4de0_7d00_8afe_d25a_63046c5f7d5d
  style 3d21bb44_e12a_666f_ff91_4155c0f8d71b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/react-reconciler/src/ReactFiberClassComponent.js lines 761–850

function mountClassInstance(
  workInProgress: Fiber,
  ctor: any,
  newProps: any,
  renderLanes: Lanes,
): void {
  if (__DEV__) {
    checkClassInstance(workInProgress, ctor, newProps);
  }

  const instance = workInProgress.stateNode;
  instance.props = newProps;
  instance.state = workInProgress.memoizedState;
  instance.refs = {};

  initializeUpdateQueue(workInProgress);

  const contextType = ctor.contextType;
  if (typeof contextType === 'object' && contextType !== null) {
    instance.context = readContext(contextType);
  } else if (disableLegacyContext) {
    instance.context = emptyContextObject;
  } else {
    const unmaskedContext = getUnmaskedContext(workInProgress, ctor, true);
    instance.context = getMaskedContext(workInProgress, unmaskedContext);
  }

  if (__DEV__) {
    if (instance.state === newProps) {
      const componentName = getComponentNameFromType(ctor) || 'Component';
      if (!didWarnAboutDirectlyAssigningPropsToState.has(componentName)) {
        didWarnAboutDirectlyAssigningPropsToState.add(componentName);
        console.error(
          '%s: It is not recommended to assign props directly to state ' +
            "because updates to props won't be reflected in state. " +
            'In most cases, it is better to use props directly.',
          componentName,
        );
      }
    }

    if (workInProgress.mode & StrictLegacyMode) {
      ReactStrictModeWarnings.recordLegacyContextWarning(
        workInProgress,
        instance,
      );
    }

    ReactStrictModeWarnings.recordUnsafeLifecycleWarnings(
      workInProgress,
      instance,
    );
  }

  instance.state = workInProgress.memoizedState;

  const getDerivedStateFromProps = ctor.getDerivedStateFromProps;
  if (typeof getDerivedStateFromProps === 'function') {
    applyDerivedStateFromProps(
      workInProgress,
      ctor,
      getDerivedStateFromProps,
      newProps,
    );
    instance.state = workInProgress.memoizedState;
  }

  // In order to support react-lifecycles-compat polyfilled components,
  // Unsafe lifecycles should not be invoked for components using the new APIs.
  if (
    typeof ctor.getDerivedStateFromProps !== 'function' &&
    typeof instance.getSnapshotBeforeUpdate !== 'function' &&
    (typeof instance.UNSAFE_componentWillMount === 'function' ||
      typeof instance.componentWillMount === 'function')
  ) {
    callComponentWillMount(workInProgress, instance);
    // If we had additional state updates during this life-cycle, let's
    // process them now.
    processUpdateQueue(workInProgress, newProps, instance, renderLanes);
    suspendIfUpdateReadFromEntangledAsyncAction();
    instance.state = workInProgress.memoizedState;

Domain

Subdomains

Frequently Asked Questions

What does mountClassInstance() do?
mountClassInstance() is a function in the react codebase, defined in packages/react-reconciler/src/ReactFiberClassComponent.js.
Where is mountClassInstance() defined?
mountClassInstance() is defined in packages/react-reconciler/src/ReactFiberClassComponent.js at line 761.
What does mountClassInstance() call?
mountClassInstance() calls 3 function(s): applyDerivedStateFromProps, callComponentWillMount, checkClassInstance.
What calls mountClassInstance()?
mountClassInstance() is called by 2 function(s): mountIncompleteClassComponent, updateClassComponent.

Analyze Your Own Codebase

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

Try Supermodel Free