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树
}
相关推荐
SuperEugene10 分钟前
TypeScript+Vue 实战:告别 any 滥用,统一接口 / Props / 表单类型,实现类型安全|编码语法规范篇
开发语言·前端·javascript·vue.js·安全·typescript
我是永恒31 分钟前
上架一个跨境工具导航网站
前端
电子羊37 分钟前
Spec 编程工作流文档
前端
GISer_Jing43 分钟前
从CLI到GUI桌面应用——前端工程化进阶之路
前端·人工智能·aigc·交互
还是大剑师兰特1 小时前
Vue3 报错:computed value is readonly 解决方案
前端·vue.js
leaves falling1 小时前
有效的字母异位词
java·服务器·前端
We་ct1 小时前
LeetCode 35. 搜索插入位置:二分查找的经典应用
前端·算法·leetcode·typescript·个人开发
左耳咚1 小时前
Claude Code 中的 SubAgent
前端·ai编程·claude
FPGA小迷弟1 小时前
高频时钟设计:FPGA 多时钟域同步与时序收敛实战方案
前端·学习·fpga开发·verilog·fpga
IT古董1 小时前
【前端】企业级前端调试体系设计(含日志埋点 + Eruda 动态注入 + Sentry)
前端·sentry