五金外贸网站模板,湖南电商网站建设,广告代理商是什么意思,广告设计网课React核心源码解析 react 特点CPU卡顿IO 卡顿 新老 react 架构对比v15v16.8Scheduler 调度器Reconciler 协调器 React fiber原理更新dommount 构建过程 render阶段 — scheduler reconcilerreact源码解析react-domreact-dom/src/client/ReactDOMRoot.js react-reconcilerreact-… React核心源码解析 react 特点CPU卡顿IO 卡顿 新老 react 架构对比v15v16.8Scheduler 调度器Reconciler 协调器  React fiber原理更新dommount 构建过程 render阶段 — scheduler reconcilerreact源码解析react-domreact-dom/src/client/ReactDOMRoot.js react-reconcilerreact-reconciler/src/ReactFiberReconciler.jsreact-reconciler/src/ReactFiberWorkLoop.jsreact-reconciler/src/ReactFiberBeginWork.jsreact-reconciler/src/ReactChildFiber.jsreact-reconciler/src/ReactFiber.jsreact-reconciler/src/ReactFiberWorkLoop.js  commit阶段 同步阶段 cline  deeseek  AI工具上手成本非常低非常便宜 cursor 编辑器 带着问题学源码 
为什么 react 会引入 fiber 架构简述 fiber 节点的结构和作用fiber 架构  架构流程diff 算法hooks 原理 
学习方法论由大到小 对比回答问题 
react 特点 
单向数据流 能够快速响应用户操作 公式ui  render(data) 
什么原因导致响应慢 cpu卡顿js执行导致画面卡顿 IO卡顿网络问题等 延迟 
CPU卡顿 
浏览器一秒60hz16.6ms刷新一次超过就会有掉帧现象 就比如这个例子 
// index.js
import ReactDOM from react-dom;import App from ./App;const rootElement  document.getElementById(root);// ReactDOM.render(App /, rootElement);
ReactDOM.createRoot(rootElement).render(App /);// APP.js
import ./styles.css;export default function App() {const len  3000;return (ul{Array(len).fill(0).map((_, i)  (li{i}/li))}/ul);
}React是怎么解决的 时间分片把更新过程碎片化 优先级 应用更新 后台预渲染 
同步 阻塞 渲染异步 非阻塞 用户优先级渲染 
16.8后提出的 concurrent mode 
ReactDOM.createRoot(rootEl).render(App /)IO 卡顿 
接口响应时间长解决 
loadingsuspence 兜底 fallbackerror-boundary 错误兜底  
新老 react 架构对比 
v15 同步的不可中断的 v16.8 异步可中断 v18 ssr升级 流式 ssr stream针对页面所有组件是分块分局部输出而不是等所有页面拼接组装好后一次性传输给客户端 页面上的不同接口包裹成流式然后传输给客户端提升TTFB(首字节响应时间) 性能优化有需求使用nextjs做提升 
v15 
Reconciler 协调器diff 负责找出变化的组件 update 更新component render jsx 渲染  vdomvdom diff找出变化的元素通知 renderer 渲染  
通过递归的方式找出变化的组件 mount 阶段  调用 mountComponent update 阶段  调用 updateComponent 
递归更新子组件  缺点层级深递归时间超过16ms 
Renderer 渲染器负责将变化的组件渲染到页面上 ReactDom.renderReactNative.render  
不可中断中断则后续内容不执行 
v16.8 
多了一个 Scheduler 调度器 
Scheduler 调度器 调度任务的优先级Reconciler 协调器 负责找出变化的组件 递归可中断Renderer 渲染器 是拿着Reconciler提供的标识 同步渲染 
Scheduler 调度器 
将大型任务分割成小任务每一帧分配一定的时间执行小任务 
时间切片优先级调度 
每个工作单元对应一个fiber节点时间分配调度循环 维护任务队列时间检查暂停与恢复利用浏览器API 
requestAnimationFrame 用于在下一帧开始的时候执行回调函数requestIdleCallBack 浏览器空闲的时候执行回调函数 setTimeout 模拟了 
Reconciler 协调器 
// 更新工作从递归变成了可以中断的循环过程。每次循环都会调用shouldYield判断当前是否有剩余时间。/** noinline */
function workLoopConcurrent() {// Perform work until Scheduler asks us to yieldwhile (workInProgress ! null  !shouldYield()) {workInProgress  performUnitOfWork(workInProgress);}
}React fiber 
原理 
react 内部实现的数据结构支持状态更新可中断可恢复恢复后可以复用之前的中间状态 
架构v15 stack reconcilerv16 fiber reconciler数据结构每个 fiber 节点对应 react element多个组件类型dom节点各种属性数据动态的工作单元改变的状态要执行的工作 
function FiberNode(tag: WorkTag,pendingProps: mixed,key: null | string,mode: TypeOfMode,
) {// Instance静态节点的数据结构属性this.tag  tag;this.key  key;this.elementType  null;this.type  null;this.stateNode  null;// Fiber用来链接其他fiber节点形成的fiber树this.return  null;this.child  null;this.sibling  null;this.index  0;this.ref  null;// 作为动态的工作单元的属性this.pendingProps  pendingProps; //即将应用到组件上新属性 相当于是新值newValue this.memoizedProps  null; // 上次渲染时使用的属性值 相当于是旧值oldValuethis.updateQueue  null;this.memoizedState  null;this.dependencies  null;this.mode  mode;// 记录变化的节点 effectlistrenderthis.effectTag  NoEffect;this.subtreeTag  NoSubtreeEffect;this.deletions  null;this.nextEffect  null;// effectslist链  firstEffect - nextEffect - nextEffect - lastEffect  交给render  渲染this.firstEffect  null;this.lastEffect  null;// 作为调度优先级的属性this.lanes  NoLanes;this.childLanes  NoLanes;// 指向该fiber在另一次更新时对应的fiberthis.alternate  null;if (enableProfilerTimer) {// Note: The following is done to avoid a v8 performance cliff.//// Initializing the fields below to smis and later updating them with// double values will cause Fibers to end up having separate shapes.// This behavior/bug has something to do with Object.preventExtension().// Fortunately this only impacts DEV builds.// Unfortunately it makes React unusably slow for some applications.// To work around this, initialize the fields below with doubles.//// Learn more about this here:// https://github.com/facebook/react/issues/14365// https://bugs.chromium.org/p/v8/issues/detail?id8538this.actualDuration  Number.NaN;this.actualStartTime  Number.NaN;this.selfBaseDuration  Number.NaN;this.treeBaseDuration  Number.NaN;// Its okay to replace the initial doubles with smis after initialization.// This wont trigger the performance cliff mentioned above,// and it simplifies other profiler code (including DevTools).this.actualDuration  0;this.actualStartTime  -1;this.selfBaseDuration  0;this.treeBaseDuration  0;}if (__DEV__) {// This isnt directly used but is handy for debugging internals:this._debugID  debugCounter;this._debugSource  null;this._debugOwner  null;this._debugNeedsRemount  false;this._debugHookTypes  null;if (!hasBadMapPolyfill  typeof Object.preventExtensions  function) {Object.preventExtensions(this);}}
}举个例子 
import React, { Component } from react;class Header extends Component {render() {return h1{this.props.title}/h1;}
}function Content(props) {return (divp{props.text}/pFooter //div);
}class Footer extends Component {render() {return footerFooter Content/footer;}
}class App extends Component {render() {return (divHeader titleWelcome to My App /Content textThis is some example content. //div);}
}export default App;Fiber结构 Root Fiber Node └── Type: “div” (elementType: “div”) ├── Child Fiber Node │ ├── Type: “Header” (elementType: Header) │ │ └── State Node: Header instance │ │ └── Props: { title: “Welcome to My App” } │ └── Sibling: Content Fiber Node └── Child Fiber Node ├── Type: “Content” (elementType: Content) │ └── State Node: null (函数组件没有状态节点) │ └── Props: { text: “This is some example content.” } │ └── Child Fiber Node │ ├── Type: “Footer” (elementType: Footer) │ │ └── State Node: Footer instance │ │ └── Props: {} │ └── Sibling: null 
更新dom 
双缓存机制 内存中绘制当前的 fiber dom绘制完后直接替换上一帧的fiber dom这样省去两帧之间替换的计算时间就不会存在白屏的情况因此就有两棵fiber树 
current fiber 屏幕上正在显示的内容workingprogress fiber 内存中正在构建的树 简称 wip fiber 
alternate 连接 mount 构建过程 
应用级别的节点 ReactDom.render 创建 fiberRootNoderootFiber 组件树的根节点 render阶段 — scheduler reconciler 
通过遍历 找到所有的fiber结构 实现可中断的异步递归 
递  生成树 创建节点形成节点之间的关系 vdom 
从 rootfiber 深度优先遍历 fiber 调用 beginwork 
根据传入的 fiber 节点创建子 fiber 节点连接两个 fiber 节点遍历叶子节点进入归的阶段 
归 调用completework 
创建真实的dom节点将当前节点下的子节点挂载到当前节点上收集当前节点的effectlist 
递 归 交错执行直到归到rootFiber  
react源码解析 
react github 
react-dom react-dom/src/client/ReactDOMRoot.js 最终返回ReactDOMRoot这个实例 
ReactDOMRoot 和 ReactDOMHydrationRoot 上面都挂载 render 方法 接收children 调用updateContainer方法传入childrenupdateContainer来进行递归的这个阶段创建当前节点  
后面还挂载了unmount方法  
react-reconciler 
react-reconciler/src/ReactFiberReconciler.js 
这里着重是 Scheduler 创建FiberRootNode节点绑定root render渲染方法传入childrenApp调用updateContainer方法进行大任务拆分小任务优先级调度  
react-reconciler/src/ReactFiberWorkLoop.js 
着重介绍循环创建fiber树的方法 核心方法performUnitOfWorkworkLoopConcurrent workLoopSync 同步方法不会判断 shouldYield这是和workLoopConcurrent方法的区别  workLoopConcurrent方法是异步模式都是调用performUnitOfWork构造fiber树  这里是Scheduler调度器将渲染任务拆分成不同的任务单元去创建对应的fiberfiber通过performUnitOfWork去完成fiber单元的创建然后通过shouldYield判断是否执行这样的任务 workInProgress是全局的变量存储在全局 
performUnitOfWork中记录当前“递”和“归”的一个过程判断当前满足条件进入beginWork 
Reconciler 协调器阶段创建fiber树递归遍历diff算法比较差异 performUnitOfWork中判断是否是开发环境开发环境开启性能调优则计算执行时间 调用beginWork进行节点的递阶段拆解组件内容并且返回下一个组件 当深度遍历到最底层的时候开始进行归的阶段 则next为空调用completeUnitOfWork开始”归“的阶段归回父节点更新父节点状态 递和归阶段是交错执行的直到回到rootFiber为止  
react-reconciler/src/ReactFiberBeginWork.js 
beginWork  if 阶段后续进入到diff的过程非首次渲染 否则为else阶段 
后面  updateHostComponent   reconcileChildren mount组件创建新的子Fiber节点   
react-reconciler/src/ReactChildFiber.js 、  通过调用useFiber创建fiber节点  
react-reconciler/src/ReactFiber.js 
找到workInProgress为null则创建Fiber节点  不论是哪个方法最终返回的都是 workInProgress.child下一个节点 
react-reconciler/src/ReactFiberWorkLoop.js 
performUnitOfWork中 上述 beginWork返回的是workInProgress.child下一个节点因此next就会发生变化 next为null时候则叶子节点为空调用completeUnitOfWork  next不为空则将next指针赋值给workInProgress修改workInProgress指向重新执行beginWork 
在 completeUnitOfWork 中创建对应的dom元素如果sibling不为null然后创建对应的指针  
commit阶段 同步阶段 
effectlist 
before mutation 阶段执行 dom操作前mutation 阶段执行dom操作阶段遍历effectlist执行mutationlayout 阶段执行dom操作后绘制 
可以自己写一个实例然后打断点看操作数据操作结果  
一句话来总结commit阶段所作的事情 基于链表的方式存储副作用并根据优先级执行这些更新直至所有的更新完成。 
React遍历fiber树并将需要执行副作用的节点以链表的形式收集起来根据优先级存储更新递归执行更新 文章转载自: http://www.morning.hsjrk.cn.gov.cn.hsjrk.cn http://www.morning.beeice.com.gov.cn.beeice.com http://www.morning.tnmmp.cn.gov.cn.tnmmp.cn http://www.morning.jgmlb.cn.gov.cn.jgmlb.cn http://www.morning.skfkx.cn.gov.cn.skfkx.cn http://www.morning.bhbxd.cn.gov.cn.bhbxd.cn http://www.morning.kwqt.cn.gov.cn.kwqt.cn http://www.morning.lhrxq.cn.gov.cn.lhrxq.cn http://www.morning.mgzjz.cn.gov.cn.mgzjz.cn http://www.morning.lktjj.cn.gov.cn.lktjj.cn http://www.morning.pjftk.cn.gov.cn.pjftk.cn http://www.morning.zqxhn.cn.gov.cn.zqxhn.cn http://www.morning.fhkr.cn.gov.cn.fhkr.cn http://www.morning.hnzrl.cn.gov.cn.hnzrl.cn http://www.morning.jpydf.cn.gov.cn.jpydf.cn http://www.morning.wdxr.cn.gov.cn.wdxr.cn http://www.morning.nlgmr.cn.gov.cn.nlgmr.cn http://www.morning.lpmdy.cn.gov.cn.lpmdy.cn http://www.morning.nytgk.cn.gov.cn.nytgk.cn http://www.morning.qzglh.cn.gov.cn.qzglh.cn http://www.morning.wlggr.cn.gov.cn.wlggr.cn http://www.morning.hytr.cn.gov.cn.hytr.cn http://www.morning.rttxx.cn.gov.cn.rttxx.cn http://www.morning.hwlk.cn.gov.cn.hwlk.cn http://www.morning.tongweishi.cn.gov.cn.tongweishi.cn http://www.morning.zxqqx.cn.gov.cn.zxqqx.cn http://www.morning.htrzp.cn.gov.cn.htrzp.cn http://www.morning.qieistand.com.gov.cn.qieistand.com http://www.morning.dqpnd.cn.gov.cn.dqpnd.cn http://www.morning.dpwcl.cn.gov.cn.dpwcl.cn http://www.morning.dzyxr.cn.gov.cn.dzyxr.cn http://www.morning.qjxkx.cn.gov.cn.qjxkx.cn http://www.morning.wgtr.cn.gov.cn.wgtr.cn http://www.morning.ymbqr.cn.gov.cn.ymbqr.cn http://www.morning.msxhb.cn.gov.cn.msxhb.cn http://www.morning.lkhgq.cn.gov.cn.lkhgq.cn http://www.morning.807yy.cn.gov.cn.807yy.cn http://www.morning.brrxz.cn.gov.cn.brrxz.cn http://www.morning.thzgd.cn.gov.cn.thzgd.cn http://www.morning.lggng.cn.gov.cn.lggng.cn http://www.morning.pbygt.cn.gov.cn.pbygt.cn http://www.morning.jrtjc.cn.gov.cn.jrtjc.cn http://www.morning.fwkjp.cn.gov.cn.fwkjp.cn http://www.morning.rnrfs.cn.gov.cn.rnrfs.cn http://www.morning.gwqcr.cn.gov.cn.gwqcr.cn http://www.morning.gmrxh.cn.gov.cn.gmrxh.cn http://www.morning.bpmfr.cn.gov.cn.bpmfr.cn http://www.morning.bby45.cn.gov.cn.bby45.cn http://www.morning.gqfbh.cn.gov.cn.gqfbh.cn http://www.morning.drcnf.cn.gov.cn.drcnf.cn http://www.morning.wmcng.cn.gov.cn.wmcng.cn http://www.morning.pznhn.cn.gov.cn.pznhn.cn http://www.morning.wjhdn.cn.gov.cn.wjhdn.cn http://www.morning.nrgdc.cn.gov.cn.nrgdc.cn http://www.morning.zrmxp.cn.gov.cn.zrmxp.cn http://www.morning.qxlyf.cn.gov.cn.qxlyf.cn http://www.morning.mydgr.cn.gov.cn.mydgr.cn http://www.morning.lnckq.cn.gov.cn.lnckq.cn http://www.morning.mdmc.cn.gov.cn.mdmc.cn http://www.morning.nlcw.cn.gov.cn.nlcw.cn http://www.morning.rqjxc.cn.gov.cn.rqjxc.cn http://www.morning.hlkxb.cn.gov.cn.hlkxb.cn http://www.morning.cplym.cn.gov.cn.cplym.cn http://www.morning.zpyxl.cn.gov.cn.zpyxl.cn http://www.morning.gmnmh.cn.gov.cn.gmnmh.cn http://www.morning.ndlww.cn.gov.cn.ndlww.cn http://www.morning.pjrgb.cn.gov.cn.pjrgb.cn http://www.morning.xkhhy.cn.gov.cn.xkhhy.cn http://www.morning.nthyjf.com.gov.cn.nthyjf.com http://www.morning.gxfpk.cn.gov.cn.gxfpk.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.xqltq.cn.gov.cn.xqltq.cn http://www.morning.mdpkf.cn.gov.cn.mdpkf.cn http://www.morning.zkqjz.cn.gov.cn.zkqjz.cn http://www.morning.ympcj.cn.gov.cn.ympcj.cn http://www.morning.rxfjg.cn.gov.cn.rxfjg.cn http://www.morning.zdhxm.com.gov.cn.zdhxm.com http://www.morning.tbbxn.cn.gov.cn.tbbxn.cn http://www.morning.gtkyr.cn.gov.cn.gtkyr.cn http://www.morning.huihuangwh.cn.gov.cn.huihuangwh.cn