Home / Function/ beginWork() — react Function Reference

beginWork() — react Function Reference

Architecture documentation for the beginWork() function in ReactFiberBeginWork.js from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  235e1db1_02a4_912f_7d82_ea6636c850c0["beginWork()"]
  0be70812_cc0c_b210_f84f_8e61dd5f831c["ReactFiberBeginWork.js"]
  235e1db1_02a4_912f_7d82_ea6636c850c0 -->|defined in| 0be70812_cc0c_b210_f84f_8e61dd5f831c
  b4e6b9a2_baf8_7349_8a18_89866b36cc91["remountFiber()"]
  235e1db1_02a4_912f_7d82_ea6636c850c0 -->|calls| b4e6b9a2_baf8_7349_8a18_89866b36cc91
  ec058345_39f3_16c0_234d_553132510c15["checkScheduledUpdateOrContext()"]
  235e1db1_02a4_912f_7d82_ea6636c850c0 -->|calls| ec058345_39f3_16c0_234d_553132510c15
  df2c7818_4611_b8d2_dfe8_a79e5972020c["attemptEarlyBailoutIfNoScheduledUpdate()"]
  235e1db1_02a4_912f_7d82_ea6636c850c0 -->|calls| df2c7818_4611_b8d2_dfe8_a79e5972020c
  dae8f65a_04a5_194b_7c97_c4819e519207["getIsHydrating()"]
  235e1db1_02a4_912f_7d82_ea6636c850c0 -->|calls| dae8f65a_04a5_194b_7c97_c4819e519207
  50e17875_d138_1098_df3a_5e5668c9be3d["updateFunctionComponent()"]
  235e1db1_02a4_912f_7d82_ea6636c850c0 -->|calls| 50e17875_d138_1098_df3a_5e5668c9be3d
  7f5a1eb6_175b_de57_e39a_6c9d67383c84["resolveClassComponentProps()"]
  235e1db1_02a4_912f_7d82_ea6636c850c0 -->|calls| 7f5a1eb6_175b_de57_e39a_6c9d67383c84
  9bea19b8_e307_63f1_e3b4_89c20a3fd20b["updateClassComponent()"]
  235e1db1_02a4_912f_7d82_ea6636c850c0 -->|calls| 9bea19b8_e307_63f1_e3b4_89c20a3fd20b
  53f55fda_e2b6_2801_4fbc_525f8828d23d["updateHostRoot()"]
  235e1db1_02a4_912f_7d82_ea6636c850c0 -->|calls| 53f55fda_e2b6_2801_4fbc_525f8828d23d
  f4acdab6_f680_af63_8940_0259a68c2f48["updateSuspenseComponent()"]
  235e1db1_02a4_912f_7d82_ea6636c850c0 -->|calls| f4acdab6_f680_af63_8940_0259a68c2f48
  34c6ce5d_a29b_1593_e4b1_ed95494a5da8["updatePortalComponent()"]
  235e1db1_02a4_912f_7d82_ea6636c850c0 -->|calls| 34c6ce5d_a29b_1593_e4b1_ed95494a5da8
  f3abfeea_868f_5c8e_9744_74c9973ded12["updateForwardRef()"]
  235e1db1_02a4_912f_7d82_ea6636c850c0 -->|calls| f3abfeea_868f_5c8e_9744_74c9973ded12
  9de08ac8_c26a_67db_9944_713b083358ad["updateFragment()"]
  235e1db1_02a4_912f_7d82_ea6636c850c0 -->|calls| 9de08ac8_c26a_67db_9944_713b083358ad
  0aa7813c_2320_24a0_dc18_24050a811a89["updateMode()"]
  235e1db1_02a4_912f_7d82_ea6636c850c0 -->|calls| 0aa7813c_2320_24a0_dc18_24050a811a89
  style 235e1db1_02a4_912f_7d82_ea6636c850c0 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/react-reconciler/src/ReactFiberBeginWork.js lines 4147–4429

function beginWork(
  current: Fiber | null,
  workInProgress: Fiber,
  renderLanes: Lanes,
): Fiber | null {
  if (__DEV__) {
    if (workInProgress._debugNeedsRemount && current !== null) {
      // This will restart the begin phase with a new fiber.
      const copiedFiber = createFiberFromTypeAndProps(
        workInProgress.type,
        workInProgress.key,
        workInProgress.pendingProps,
        workInProgress._debugOwner || null,
        workInProgress.mode,
        workInProgress.lanes,
      );
      copiedFiber._debugStack = workInProgress._debugStack;
      copiedFiber._debugTask = workInProgress._debugTask;
      return remountFiber(current, workInProgress, copiedFiber);
    }
  }

  if (current !== null) {
    const oldProps = current.memoizedProps;
    const newProps = workInProgress.pendingProps;

    if (
      oldProps !== newProps ||
      hasLegacyContextChanged() ||
      // Force a re-render if the implementation changed due to hot reload:
      (__DEV__ ? workInProgress.type !== current.type : false)
    ) {
      // If props or context changed, mark the fiber as having performed work.
      // This may be unset if the props are determined to be equal later (memo).
      didReceiveUpdate = true;
    } else {
      // Neither props nor legacy context changes. Check if there's a pending
      // update or context change.
      const hasScheduledUpdateOrContext = checkScheduledUpdateOrContext(
        current,
        renderLanes,
      );
      if (
        !hasScheduledUpdateOrContext &&
        // If this is the second pass of an error or suspense boundary, there
        // may not be work scheduled on `current`, so we check for this flag.
        (workInProgress.flags & DidCapture) === NoFlags
      ) {
        // No pending updates or context. Bail out now.
        didReceiveUpdate = false;
        return attemptEarlyBailoutIfNoScheduledUpdate(
          current,
          workInProgress,
          renderLanes,
        );
      }
      if ((current.flags & ForceUpdateForLegacySuspense) !== NoFlags) {
        // This is a special case that only exists for legacy mode.
        // See https://github.com/facebook/react/pull/19216.
        didReceiveUpdate = true;
      } else {
        // An update was scheduled on this fiber, but there are no new props
        // nor legacy context. Set this to false. If an update queue or context
        // consumer produces a changed value, it will set this to true. Otherwise,
        // the component will assume the children have not changed and bail out.
        didReceiveUpdate = false;
      }
    }
  } else {
    didReceiveUpdate = false;

    if (getIsHydrating() && isForkedChild(workInProgress)) {
      // Check if this child belongs to a list of muliple children in
      // its parent.
      //
      // In a true multi-threaded implementation, we would render children on
      // parallel threads. This would represent the beginning of a new render
      // thread for this subtree.
      //
      // We only use this for id generation during hydration, which is why the
      // logic is located in this special branch.

Domain

Subdomains

Frequently Asked Questions

What does beginWork() do?
beginWork() is a function in the react codebase, defined in packages/react-reconciler/src/ReactFiberBeginWork.js.
Where is beginWork() defined?
beginWork() is defined in packages/react-reconciler/src/ReactFiberBeginWork.js at line 4147.
What does beginWork() call?
beginWork() calls 28 function(s): attemptEarlyBailoutIfNoScheduledUpdate, checkScheduledUpdateOrContext, getIsHydrating, mountIncompleteClassComponent, mountIncompleteFunctionComponent, remountFiber, resolveClassComponentProps, updateActivityComponent, and 20 more.

Analyze Your Own Codebase

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

Try Supermodel Free