react原理

2021-04-17  本文已影响0人  DoEmpty

基本概念

fiber之前的react

babel在编译jsx代码的时候,会将其转换成React.createElement方法,运行时得到一个虚拟dom树
react会递归比对虚拟dom树,找出需要变动的节点,同步更新它们。这个过程称之为Reconcilation(协调)
在协调期间,React会一直占用浏览器资源,不能够中断,当一个项目足够大的时候,会有很深的调用栈,而且长时间无法释放浏览器资源,造成卡顿

 const root = {
   key: 'a1',
   children: [
     {
       key: 'b1',
       children: [
         {
           key: 'c1',
           children: [],
         }, 
         {
           key: 'c2',
           children: [],
         }
       ]
     },
     {
       key: 'b2',
       children: []
     }
   ]
 }

 walk(root);

 function walk(vDom) {
   doWork(vDom);
   vDom.children.forEach(child => {
     walk(child);
   })
 }

 function doWork(vDom) {
   console.log(vDom.key);
 }

fiber是什么

fiber链表结构描述了上述root结构的一个虚拟dom树

fiber执行阶段

上一篇 下一篇

猜你喜欢

热点阅读