Vue & React

Vue 的源码主要分为以下几个部分:

主要涉及 响应式、虚拟 DOM、组件系统、编译器、运行时

复制代码
├── packages/
│   ├── compiler-core/    # 编译器核心
│   ├── compiler-sfc/     # 处理 .vue 单文件组件
│   ├── compiler-dom/     # 处理 DOM 相关的编译逻辑
│   ├── reactivity/       # 响应式系统
│   ├── runtime-core/     # 运行时核心
│   ├── runtime-dom/      # 运行时 DOM 相关
│   ├── shared/           # 共享工具函数
│   ├── vue/              # Vue 入口
│   └── ...

React 源码结构:

整体架构可以分为 调度(Scheduler)、协调(Reconciler)、渲染(Renderer) 三个核心部分

可以从 React 入口、Fiber 架构、调度机制、Hooks 实现、Diff 算法 等方面解析其核心原理。

复制代码
├── packages/
│   ├── react/            # React 核心 API(React.createElement、hooks)
│   ├── react-dom/        # 负责渲染到 DOM
│   ├── scheduler/        # 调度器,控制任务优先级
│   ├── react-reconciler/ # 负责 Fiber 树的协调和 Diff
│   ├── shared/           # 公共方法
│   ├── jest/             # 测试相关
│   └── ...

Fiber 是 React 16 引入的核心数据结构,每个组件对应一个 Fiber 节点:

javascript 复制代码
function FiberNode(
  tag: WorkTag,
  pendingProps: mixed,
  key: null | string,
  mode: TypeOfMode,
) {
  // 实例相关
  this.tag = tag;           // 组件类型(Function/Class/Host等)
  this.key = key;           // key属性
  this.elementType = null;  // 创建元素的类型
  this.type = null;         // 组件函数/类
  this.stateNode = null;    // 对应的真实DOM实例/类组件实例

  // Fiber树结构
  this.return = null;       // 父Fiber
  this.child = null;        // 第一个子Fiber
  this.sibling = null;      // 兄弟Fiber
  this.index = 0;           // 在兄弟中的索引

  // 状态相关
  this.pendingProps = pendingProps;
  this.memoizedProps = null;
  this.updateQueue = null;  // 状态更新队列
  this.memoizedState = null;// 当前状态

  // 副作用
  this.effectTag = NoEffect;
  this.nextEffect = null;   // 下一个有副作用的Fiber

  // 双缓存技术
  this.alternate = null;    // 连接current和workInProgress树
}
相关推荐
陈_杨几秒前
鸿蒙5开发宝藏案例分享---切面编程实战揭秘
前端
喵手7 分钟前
CSS3 渐变、阴影和遮罩的使用
前端·css·css3
顽强d石头9 分钟前
bug:undefined is not iterable (cannot read property Symbol(Symbol.iterator))
前端·bug
烛阴18 分钟前
模块/命名空间/全局类型如何共存?TS声明空间终极生存指南
前端·javascript·typescript
火车叼位22 分钟前
Git 精准移植代码:cherry-pick 简单说明
前端·git
江城开朗的豌豆25 分钟前
JavaScript篇:移动端点击的300ms魔咒:你以为用户手抖?其实是浏览器在搞事情!
前端·javascript·面试
华洛32 分钟前
聊聊我们公司的AI应用工程师每天都干啥?
前端·javascript·vue.js
江城开朗的豌豆32 分钟前
JavaScript篇:你以为事件循环都一样?浏览器和Node的差别让我栽了跟头!
前端·javascript·面试
技术小丁35 分钟前
使用 HTML +JavaScript 从零构建视频帧提取器
javascript·html·音视频
gyx_这个杀手不太冷静35 分钟前
Vue3 响应式系统探秘:watch 如何成为你的数据侦探
前端·vue.js·架构