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树
}
相关推荐
90后的晨仔1 分钟前
从 H5 到 uni-app:一篇写给前端小白的"翻译指南"
前端·vue.js·前端框架
陈随易30 分钟前
moon,apt和yum之外linux系统命令安装新选择
前端·后端·程序员
IT小盘1 小时前
13-企业Prompt模板-角色任务约束与输出格式
java·前端·prompt
徐小夕2 小时前
开源!我用SQLite + DuckDB打造了一款可视化AI问数平台
前端·算法·github
leslie1182 小时前
babel笔记
前端
用户059540174462 小时前
Redis 记忆存储踩坑实录:一个并发写入 Bug 让我排查了 4 小时
前端·css
小徐_23333 小时前
Wot UI 2.3.0 发布:二维码组件来了,Open Wot 与 wot-starter 同步更新
前端·微信小程序·uni-app
kyriewen3 小时前
Claude自己跑出去hack了3家公司——我为什么还在用它写代码
前端·ai编程·claude
IT_陈寒3 小时前
SpringBoot自动配置的坑,这次真踩疼我了
前端·人工智能·后端
子兮曰4 小时前
AI 浏览器 Agent 的安全困局:当自动化工具可以偷走你的登录态
前端·人工智能·后端