实现 React 函数组件渲染

基于文章 实现 React 类组件渲染

本文将介绍如何渲染函数组件。

js 复制代码
function FunctionComponent() {
    return (
        <div>
            <h3>FunctionComponent<h3>
        </div>
    )
}

Render 阶段

BeginWork 阶段

beginWork 函数增加函数组件的 case。

js 复制代码
function updateFunctionComponent(current: Fiber | null, workInProgress: Fiber) {
  const { type, pendingProps } = workInProgress;
  // 函数执行结果就是 children
  const children = type(pendingProps);
  reconcileChildren(current, workInProgress, children);
  return workInProgress.child;
}

// 1. 处理当前 fiber,因为不同组件对应的 fiber 处理方式不同,
// 2. 返回子节点 child
export function beginWork(
  current: Fiber | null,
  workInProgress: Fiber
): Fiber | null {
  switch (workInProgress.tag) {
    // 根节点
    case HostRoot:
      return updateHostRoot(current, workInProgress);
    // 原生标签,div、span...
    case HostComponent:
      return updateHostComponent(current, workInProgress);
    // 文本节点
    case HostText:
      return updateHostText(current, workInProgress);
    // Fragment
    case Fragment:
      return updateHostFragment(current, workInProgress);
    case ClassComponent:
      return updateClassComponent(current, workInProgress);
    case FunctionComponent:
      return updateFunctionComponent(current, workInProgress);
  }
  throw new Error(
    `Unknown unit of work tag (${workInProgress.tag}). This error is likely caused by a bug in ` +
      "React. Please file an issue."
  );
}

createFiberFromTypeAndProps 函数,增加函数组件的 case。

js 复制代码
// 根据 TypeAndProps 创建fiber
export function createFiberFromTypeAndProps(
  type: any,
  key: null | string,
  pendingProps: any
) {
  let fiberTag: WorkTag = IndeterminateComponent;

  if (isFn(type)) {
    // 函数组件、类组件
    if (type.prototype.isReactComponent) {
      fiberTag = ClassComponent;
    } else {
      fiberTag = FunctionComponent;
    }
  } else if (isStr(type)) {
    // 原生标签
    fiberTag = HostComponent;
  } else if (type === REACT_FRAGMENT_TYPE) {
    fiberTag = Fragment;
  }
  const fiber = createFiber(fiberTag, pendingProps, key);
  fiber.elementType = type;
  fiber.type = type;
  return fiber;
}

Complete 阶段

completeWork 函数增加函数组件的 case。

js 复制代码
function completeWork(
  current: Fiber | null,
  workInProgress: Fiber
): Fiber | null {
  const newProps = workInProgress.pendingProps;
  switch (workInProgress.tag) {
    case Fragment:
    case HostRoot:
    case ClassComponent:
    case FunctionComponent:{
      return null;
    }
    case HostComponent: {
      // 原生标签,type 是标签名
      const { type } = workInProgress;
        // 1. 创建真实 DOM
        const instance = document.createElement(type);
        // 2. 初始化 DOM 属性
        finalizeInitialChildren(instance, newProps);
        // 3. 把子 dom 挂载到父 dom 上
        appendAllChildren(instance, workInProgress);
        workInProgress.stateNode = instance;
      return null;
    }
    case HostText: {
      workInProgress.stateNode = document.createTextNode(newProps);
      return null;
    }
  }

  throw new Error(
    `Unknown unit of work tag (${workInProgress.tag}). This error is likely caused by a bug in ` +
      "React. Please file an issue."
  );
}
相关推荐
C澒5 分钟前
IntelliPro 企业级产研协作平台:数据可视化全流程拆解
前端·数据可视化
蓝黑20208 分钟前
Vue组件通信之slot
前端·javascript·vue
小李子呢021113 分钟前
前端八股7--- Vue 状态管理工具(vuex和pinia)
前端·javascript·vue.js
Geoking.16 分钟前
后端Long型数据传到前端js后精度丢失的问题(前后端传输踩坑指南)
java·前端·javascript·后端
oi..16 分钟前
CSRF安全攻防:Referer 校验与 Token 防护详解
前端·网络·笔记·测试工具·安全·网络安全·csrf
申耀的科技观察16 分钟前
【观察】昂瑞微5G射频前端通过车规认证,筑牢智能网联汽车通信安全“底座”
前端·5g·汽车
qq_2602412316 分钟前
将盾CDN:Web应用防火墙(WAF)的工作原理与实战配置
前端·网络·安全
旺王雪饼 www17 分钟前
《Express框架深度解析:从基础入门到高级实践与项目架构》
前端·node.js·express
stpzhf19 分钟前
uniapp nvue组件多个text在一行并且高亮其中一些文字
前端·javascript·uni-app
十一.36626 分钟前
003-004 虚拟DOM的两种创建方式、虚拟DOM与真实DOM
前端·javascript·html