React16源码: React中的FunctionComponent的源码实现

Function Component

1 )概述

  • FunctionComponent 会引出调和子节点的过程
  • 调和子节点是一个统一的过程,和各类组件不同类型没有太大关系
    • 在 ReactDOM.render 第一次时,只有一个Fiber对象
    • 这个Fiber对象它会保存它的props, 里面有很多的children
    • 里面的 props.children 是 App 对应的 ReactElement
    • 这个 ReactElement 不是 Fiber 对象,肯定要去创建它的Fiber对象
    • 在 ReactDom.render 的过程中, 是一步一步的往下执行
    • 对于children,肯定要判断 ReactElement 的类型
    • 基于不同类型做对应的处理方式,这个过程肯定是非常重要的

2 )源码

定位到 packages/react-reconciler/src/ReactFiberBeginWork.js

的 beginWork 函数,在 beginWork 下面的 执行 switch case 中

js 复制代码
// beginWork 函数中的截取部分
case FunctionComponent: {
  // 这里的 type 就是 ReactElement 里面存储的 type 属性
  // 这个 type 属性里面存储的就是 组件,如果是dom原生组件就是字符串
  // 如果组件是 function component 就是一个方法,如果是 class component 它就是类
  const Component = workInProgress.type;
  // pendingProps 是新渲染的时候产生的 props,这个和调和子节点有关系
  const unresolvedProps = workInProgress.pendingProps;
  // 这个 resolvedProps 是和 Suspense 组件有关的,throw 的是一个 Promise 可能会resolve出一些东西
  // 可以先默认 resolvedProps 就是上面的 unresolvedProps
  const resolvedProps =
    workInProgress.elementType === Component
      ? unresolvedProps
      : resolveDefaultProps(Component, unresolvedProps);
  return updateFunctionComponent(
    current,
    workInProgress,
    Component,
    resolvedProps,
    renderExpirationTime,
  );
}

进入到 updateFunctionComponent 方法

js 复制代码
function updateFunctionComponent(
  current,
  workInProgress,
  Component,
  nextProps: any,
  renderExpirationTime,
) {
  // 获取两个 context 先跳过
  const unmaskedContext = getUnmaskedContext(workInProgress, Component, true);
  const context = getMaskedContext(workInProgress, unmaskedContext);

  let nextChildren;
  prepareToReadContext(workInProgress, renderExpirationTime);
  // 跳过 DEV 相关
  if (__DEV__) {
    ReactCurrentOwner.current = workInProgress;
    ReactCurrentFiber.setCurrentPhase('render');
    nextChildren = Component(nextProps, context);
    ReactCurrentFiber.setCurrentPhase(null);
  } else {
    // 这里的 Component 就是组件的方法
    // 调用 Component 方法的时候传入 nextProps, context 两个参数
    // context 这个在 官网API文档中没有提及
    // 可以在 function component 中获取到 child组件的 contextTypes 属性,也就是组件加上这个属性并配置props
    // 组件内就可以获取上层传递过来的 context 
    nextChildren = Component(nextProps, context);
  }

  // React DevTools reads this flag.
  // 注意这里
  workInProgress.effectTag |= PerformedWork;
  reconcileChildren(
    current,
    workInProgress,
    nextChildren,
    renderExpirationTime,
  );
  // 这里 return 的是 child, 说明 在 reconcileChildren 很可能处理 child
  return workInProgress.child;
}
  • 关于 workInProgress.effectTag |= PerformedWork; 这种操作
    • 在 ReactEffectTags.js 文件中, 有所有可能产生effect的值
    • 这里都是二进制的,每个值的1所在位置都是不一样的
    • 通过逻辑操作,&, | 的联合判断是否有某个特性
    • 上面 PerformedWork 本质是没有什么意义的
    • 是给 DevTools 说明这个组件在渲染中有更新
  • 关于 reconcileChildren 是通用的调节子节点的核心流程,这块先跳过
相关推荐
沙漠之主2 小时前
C++编程教学设计资料:从入门到实战的完整课程方案
java·前端·c++
hasty2 小时前
不上传新包,也能改变用户拿到的版本:npm dist-tag 的 OIDC 权限治理
前端·npm·node.js
Csvn2 小时前
diff 算法(虚拟 DOM Reconciliation)
前端
前端snow3 小时前
ai agent --- 文件存储
前端
Frag0ut3 小时前
Chrome四大版本获取及共存指南:Stable/Beta/Dev/Canary
前端·chrome·浏览器·dev·beta·canary·共存版
IT_陈寒5 小时前
SpringBoot自动配置坑了我一把,原来是这样绕过去的
前端·人工智能·后端
广州华水科技5 小时前
单北斗GNSS变形监测在大坝安全监测中的应用与优势
前端
zeng不错5 小时前
B站 173 个投稿活动看到眼瞎?我写了个扩展,主打一个精准打击
前端
Latchh5 小时前
前端导出的JPEG红字发糊,质量要拉到100才清楚
前端·图像处理·人工智能·计算机视觉
默_笙6 小时前
🚦 请三个 AI 考官给 RAG 打分:量化评估实战(下)
前端·javascript