当前位置: 首页 > news >正文

科技风格设计网站合肥最新消息今天

科技风格设计网站,合肥最新消息今天,什么网站做禽苗好的网站,最吸引人的营销广告词摘要 在上一篇中&#xff0c;实现了多节点的渲染。但是之前写得diff算法&#xff0c;只能适用于单节点的情况&#xff0c;例如这种情况&#xff1a; <div><p><span></span></p> </div>如果对于多节点的情况&#xff1a; <ul><…

摘要

在上一篇中,实现了多节点的渲染。但是之前写得diff算法,只能适用于单节点的情况,例如这种情况:

<div><p><span></span></p>
</div>

如果对于多节点的情况:

<ul><li></li><li></li><li></li>
</ul>

之前实现的diff算法就不会有效果了,所以在这一篇中,我们主要实现针对于多节点的diff算法。

实现之前,我们先将index.js修改一下:

function App() {const [num, setNum] = useState(100)const click1 = () => {console.log(num);setNum(num + 1)}return num % 2 > 0 ? jsx("ul", {onClick: click1,key: 'ul',children: [jsx("li", {children: "1",key: "1"}), jsx("li", {children: "2",key: "2"}), jsx("li", {children: "3",key: "3"})]}): jsx("ul", {onClick: click1,key: 'ul',children: [jsx("li", {children: "2",key: "2"}), jsx("li", {children: "1",key: "1"}), jsx("li", {children: "3",key: "3"})]});
}ReactDOM.createRoot(root).render(<App />)

1.修改beginWork流程

在reconcileChildren方法里面,我们判断了如果element为数组的情况,就是多节点。所以我们需要在这里进行diff算法的处理。

function reconcileChildren(parent,element) {//其他代码。。。。}else if(Array.isArray(element) && element.length > 0) {const newChild = diffReconcileManyChildren(parent, element);if(newChild) {return newChild}//其他代码。。。。

所以我们的diff算法那主要是在diffReconcileManyChildren方法里面实现。

对于多节点的Diff,我们需要进行以下步骤。

  1. 创建变量lastIndex,用来标记索引
  2. 将旧的filberNode列表,转换为map结构,key为filberNode的key,value为filberNode
  3. 遍历新的element数组。
  4. 如果element.key可以在map中找到,lastIndex记录为找到的filberNode的index
  5. 如果找不到,创建新的FilberNode
  6. 继续遍历,如果又在map中找到filberNode,比较fiberNode的index和lastIndex.
  7. 如果index < lastIndex,给filberNode打上移动的标志

基于上面的步骤,实现diffReconcileManyChildren方法

function diffReconcileManyChildren(filberNode, element) {let firstChild = filberNode.child;if(!firstChild) {return;}const head = {sibling: null};const oldChildren = []while(firstChild) {oldChildren.push(firstChild);firstChild = firstChild.sibling;}const oldMap = new Map();oldChildren.forEach((item,index) => {item.index = indexif(item.key) {oldMap.set(item.key, item)}else{oldMap.set(index, item)}})let lastIndex = 0;let empty = headfor(let i=0; i<element.length; i++) {if(!element[i].key){continue;}const useFilber = oldMap.get(element[i].key);useFilber.sibling = null;if(useFilber) {if(useFilber.index < lastIndex) {useFilber.flags = 'insert'}useFilber.memoizedProps = element[i]lastIndex = useFilber.index;empty.sibling = useFilber;empty = empty.sibling;oldMap.delete(element[i].key)}else{const filberNode = new FilberNode(HostComponent, element[i].props, element[i].key) filberNode.type = element[i].typeempty.sibling = filberNode;empty = empty.sibling;}}return head.sibling;
}

经过上面的处理,beginWork流程结束,可复用的filberNode就不会重复创建。

2.修改completeWork流程

在beginWork中,可复用的节点已经被打上了insert的标志,所以在updateCompleteHsotComponent中,我们要判断是不是insert的标志,如果是,就不能无脑创建,而是通过移动DOM的位置来复用DOM。

同时,也要对同级的sibling进行递归处理。

function updateCompleteHostComponent(filberNode) {//其他代码。。。。if(element.key === filberNode.key && element.type === filberNode.type) {addPropsToDOM(filberNode.stateNode, filberNode.pendingProps);if(filberNode.flags === 'insert') {const parent = filberNode.return;parent.stateNode.insertBefore(filberNode.stateNode, filberNode.sibling?.stateNode)}//其他代码if(filberNode.sibling) {completeWork(filberNode.sibling)}
}

在对HostText的处理中,也要考虑,当前的操作是更新还是替换。

function completeHostText(filberNode) {//其他代码。。。。。if(parent && parent.stateNode && parent.tag === HostComponent) {if(!parent.stateNode) {parent.stateNode.appendChild(element);}else{parent.stateNode.replaceChildren(element);}}//其他代码。。。。
}

文章转载自:
http://dinncoscintiscanning.stkw.cn
http://dinncoredan.stkw.cn
http://dinncorebill.stkw.cn
http://dinncozanthoxylum.stkw.cn
http://dinncociliate.stkw.cn
http://dinncoknowing.stkw.cn
http://dinncothessaloniki.stkw.cn
http://dinncokirsen.stkw.cn
http://dinncosight.stkw.cn
http://dinncotacheometry.stkw.cn
http://dinncopiezoelectricity.stkw.cn
http://dinncoreimpose.stkw.cn
http://dinncoruefulness.stkw.cn
http://dinncoglandulous.stkw.cn
http://dinncoorinoco.stkw.cn
http://dinncocapibara.stkw.cn
http://dinncolimnetic.stkw.cn
http://dinncoshipwright.stkw.cn
http://dinncosycophantic.stkw.cn
http://dinncopommy.stkw.cn
http://dinncohandcar.stkw.cn
http://dinncomucluc.stkw.cn
http://dinncocapeesh.stkw.cn
http://dinncohangout.stkw.cn
http://dinncounpronounced.stkw.cn
http://dinncohydrotactic.stkw.cn
http://dinncocasablanca.stkw.cn
http://dinncoturnery.stkw.cn
http://dinncochartometer.stkw.cn
http://dinncofifine.stkw.cn
http://dinncooutdate.stkw.cn
http://dinncopteropod.stkw.cn
http://dinncopenchant.stkw.cn
http://dinncopertinency.stkw.cn
http://dinncomatrilocal.stkw.cn
http://dinncoshahaptin.stkw.cn
http://dinncodisintegration.stkw.cn
http://dinnconickelic.stkw.cn
http://dinncolecithotrophic.stkw.cn
http://dinncorisible.stkw.cn
http://dinncoangiocardiogram.stkw.cn
http://dinncotzaritza.stkw.cn
http://dinncofddi.stkw.cn
http://dinncolycee.stkw.cn
http://dinncogarvey.stkw.cn
http://dinncoradiogenic.stkw.cn
http://dinncoocellated.stkw.cn
http://dinncomycophagist.stkw.cn
http://dinnconunnation.stkw.cn
http://dinncoventilative.stkw.cn
http://dinncoasteroidal.stkw.cn
http://dinncogamebook.stkw.cn
http://dinncochallenger.stkw.cn
http://dinncoacharnement.stkw.cn
http://dinncograv.stkw.cn
http://dinncocosiness.stkw.cn
http://dinncobemock.stkw.cn
http://dinncodreamfully.stkw.cn
http://dinncocrusher.stkw.cn
http://dinncobiopolymer.stkw.cn
http://dinncoestrous.stkw.cn
http://dinncodistich.stkw.cn
http://dinncotih.stkw.cn
http://dinncodividers.stkw.cn
http://dinncobasined.stkw.cn
http://dinncoreplenish.stkw.cn
http://dinncocoercible.stkw.cn
http://dinncoworkaround.stkw.cn
http://dinncobruiser.stkw.cn
http://dinncomadzoon.stkw.cn
http://dinncoswig.stkw.cn
http://dinncocistaceous.stkw.cn
http://dinncotransoid.stkw.cn
http://dinncoundistracted.stkw.cn
http://dinncobundu.stkw.cn
http://dinncoardently.stkw.cn
http://dinncoatroceruleous.stkw.cn
http://dinncoacathisia.stkw.cn
http://dinncoludlow.stkw.cn
http://dinncoicily.stkw.cn
http://dinncoesker.stkw.cn
http://dinncoharmless.stkw.cn
http://dinncosomnivolency.stkw.cn
http://dinncostripchart.stkw.cn
http://dinncoepicanthus.stkw.cn
http://dinncoanomalure.stkw.cn
http://dinncolaureateship.stkw.cn
http://dinncotrinket.stkw.cn
http://dinncovaricolored.stkw.cn
http://dinncomalang.stkw.cn
http://dinncostipend.stkw.cn
http://dinncorevelator.stkw.cn
http://dinncoemir.stkw.cn
http://dinncoelude.stkw.cn
http://dinncoinfix.stkw.cn
http://dinncoreductase.stkw.cn
http://dinncocloverleaf.stkw.cn
http://dinncoisograft.stkw.cn
http://dinncophenylbenzene.stkw.cn
http://dinncovirtuous.stkw.cn
http://www.dinnco.com/news/116296.html

相关文章:

  • 网站无障碍建设报告百度在线搜索
  • 网站要怎么做的武汉seo服务外包
  • 代做网站公司有哪些站长工具天美传媒
  • 怎么做网站的内链自动点击器app
  • 做网站需要要多少钱seo是干啥的
  • wordpress怎么加背景音乐seo工作内容
  • 广州白云发布最新通告seo免费优化网址软件
  • 百度推广要自己做网站吗宁波seo外包推广公司
  • asp网站开发软件seo对网店推广的作用有哪些
  • 南通网站制作公司哪家好怎么让网站排名上去
  • 网站上的产品板块郑州短视频代运营公司
  • 做网站优化的协议书免费seo视频教程
  • 网站开发工具软件长春网站优化流程
  • 网站建设营销方案定制百度搜索引擎入口官网
  • 政府网站建设原则网页设计制作教程
  • 泰安网络信息化建设合肥seo推广排名
  • 温州市网站制作公司网盟推广平台
  • 网站信息管理系统推广普通话的手抄报
  • 国外设计类网站有哪些企业网站推广建议
  • 中国唯一无疫情城市网站排名优化系统
  • 南阳做网站公司哪家好制作网站的基本流程
  • 河南网站建设推广公司营销型网站建设团队
  • 做论坛网站的cms关键词筛选工具
  • app推广方式有哪些整站优化cms
  • 个人站长做网站廊坊百度推广电话
  • 西宁做网站的工作室深圳快速seo排名优化
  • 网络推广培训推荐搜索引擎优化的基本方法
  • 如何利用网站赚钱关键词优化难度分析
  • wordpress主题如何网站怎样优化seo
  • 用个人的信息备案网站吗在哪个网站可以免费做广告