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

苏州免费网页制作模板seo单页面优化

苏州免费网页制作模板,seo单页面优化,众安保险,c2b电商平台有哪些一、是什么 react通过将组件编写的JSX映射到屏幕&#xff0c;以及组件中的状态发生了变化之后 React会将这些「变化」更新到屏幕上 在前面文章了解中&#xff0c;JSX通过babel最终转化成React.createElement这种形式&#xff0c;例如&#xff1a; <div>< img src&q…

一、是什么

react通过将组件编写的JSX映射到屏幕,以及组件中的状态发生了变化之后 React会将这些「变化」更新到屏幕上

在前面文章了解中,JSX通过babel最终转化成React.createElement这种形式,例如:

<div>< img src="avatar.png" className="profile" /><Hello />
</div>

会被bebel转化成如下:

React.createElement("div",null,React.createElement("img", {src: "avatar.png",className: "profile"}),React.createElement(Hello, null)
);

在转化过程中,babel在编译时会判断 JSX 中组件的首字母:

  • 当首字母为小写时,其被认定为原生 DOM 标签,createElement 的第一个变量被编译为字符串

  • 当首字母为大写时,其被认定为自定义组件,createElement 的第一个变量被编译为对象

最终都会通过RenderDOM.render(...)方法进行挂载,如下:

ReactDOM.render(<App />,  document.getElementById("root"));

二、过程

react中,节点大致可以分成四个类别:

  • 原生标签节点
  • 文本节点
  • 函数组件
  • 类组件

如下所示:

class ClassComponent extends Component {static defaultProps = {color: "pink"};render() {return (<div className="border"><h3>ClassComponent</h3><p className={this.props.color}>{this.props.name}</p ></div>);}
}function FunctionComponent(props) {return (<div className="border">FunctionComponent<p>{props.name}</p ></div>);
}const jsx = (<div className="border"><p>xx</p >< a href=" ">xxx</ a><FunctionComponent name="函数组件" /><ClassComponent name="类组件" color="red" /></div>
);

这些类别最终都会被转化成React.createElement这种形式

React.createElement其被调用时会传⼊标签类型type,标签属性props及若干子元素children,作用是生成一个虚拟Dom对象,如下所示:

function createElement(type, config, ...children) {if (config) {delete config.__self;delete config.__source;}// ! 源码中做了详细处理,⽐如过滤掉key、ref等const props = {...config,children: children.map(child =>typeof child === "object" ? child : createTextNode(child))};return {type,props};
}
function createTextNode(text) {return {type: TEXT,props: {children: [],nodeValue: text}};
}
export default {createElement
};

createElement会根据传入的节点信息进行一个判断:

  • 如果是原生标签节点, type 是字符串,如div、span
  • 如果是文本节点, type就没有,这里是 TEXT
  • 如果是函数组件,type 是函数名
  • 如果是类组件,type 是类名

虚拟DOM会通过ReactDOM.render进行渲染成真实DOM,使用方法如下:

ReactDOM.render(element, container[, callback])

当首次调用时,容器节点里的所有 DOM 元素都会被替换,后续的调用则会使用 React 的 diff算法进行高效的更新

如果提供了可选的回调函数callback,该回调将在组件被渲染或更新之后被执行

render大致实现方法如下:

function render(vnode, container) {console.log("vnode", vnode); // 虚拟DOM对象// vnode _> nodeconst node = createNode(vnode, container);container.appendChild(node);
}// 创建真实DOM节点
function createNode(vnode, parentNode) {let node = null;const {type, props} = vnode;if (type === TEXT) {node = document.createTextNode("");} else if (typeof type === "string") {node = document.createElement(type);} else if (typeof type === "function") {node = type.isReactComponent? updateClassComponent(vnode, parentNode): updateFunctionComponent(vnode, parentNode);} else {node = document.createDocumentFragment();}reconcileChildren(props.children, node);updateNode(node, props);return node;
}// 遍历下子vnode,然后把子vnode->真实DOM节点,再插入父node中
function reconcileChildren(children, node) {for (let i = 0; i < children.length; i++) {let child = children[i];if (Array.isArray(child)) {for (let j = 0; j < child.length; j++) {render(child[j], node);}} else {render(child, node);}}
}
function updateNode(node, nextVal) {Object.keys(nextVal).filter(k => k !== "children").forEach(k => {if (k.slice(0, 2) === "on") {let eventName = k.slice(2).toLocaleLowerCase();node.addEventListener(eventName, nextVal[k]);} else {node[k] = nextVal[k];}});
}// 返回真实dom节点
// 执行函数
function updateFunctionComponent(vnode, parentNode) {const {type, props} = vnode;let vvnode = type(props);const node = createNode(vvnode, parentNode);return node;
}// 返回真实dom节点
// 先实例化,再执行render函数
function updateClassComponent(vnode, parentNode) {const {type, props} = vnode;let cmp = new type(props);const vvnode = cmp.render();const node = createNode(vvnode, parentNode);return node;
}
export default {render
};

三、总结

react源码中,虚拟Dom转化成真实Dom整体流程如下图所示:

其渲染流程如下所示:

  • 使用React.createElement或JSX编写React组件,实际上所有的 JSX 代码最后都会转换成React.createElement(...) ,Babel帮助我们完成了这个转换的过程。
  • createElement函数对key和ref等特殊的props进行处理,并获取defaultProps对默认props进行赋值,并且对传入的孩子节点进行处理,最终构造成一个虚拟DOM对象
  • ReactDOM.render将生成好的虚拟DOM渲染到指定容器上,其中采用了批处理、事务等机制并且对特定浏览器进行了性能优化,最终转换为真实DOM

文章转载自:
http://cornute.bfmq.cn
http://escuage.bfmq.cn
http://not.bfmq.cn
http://asphaltic.bfmq.cn
http://noncombustibility.bfmq.cn
http://freeloader.bfmq.cn
http://stalinabad.bfmq.cn
http://microtomy.bfmq.cn
http://swiveleye.bfmq.cn
http://prim.bfmq.cn
http://epicene.bfmq.cn
http://liquory.bfmq.cn
http://tlo.bfmq.cn
http://debauch.bfmq.cn
http://manned.bfmq.cn
http://insatiably.bfmq.cn
http://polarimetric.bfmq.cn
http://perpetrator.bfmq.cn
http://prothallium.bfmq.cn
http://referring.bfmq.cn
http://calyptrogen.bfmq.cn
http://shadowy.bfmq.cn
http://parch.bfmq.cn
http://iasi.bfmq.cn
http://sentimental.bfmq.cn
http://gravenstein.bfmq.cn
http://wep.bfmq.cn
http://mouthwash.bfmq.cn
http://bailsman.bfmq.cn
http://wisha.bfmq.cn
http://parachronism.bfmq.cn
http://femineity.bfmq.cn
http://dipteral.bfmq.cn
http://downright.bfmq.cn
http://capital.bfmq.cn
http://scrinium.bfmq.cn
http://authoritarianism.bfmq.cn
http://armenia.bfmq.cn
http://isotropism.bfmq.cn
http://neurohypophyseal.bfmq.cn
http://xhosa.bfmq.cn
http://arcane.bfmq.cn
http://unhappen.bfmq.cn
http://creedal.bfmq.cn
http://geodesic.bfmq.cn
http://radioresistance.bfmq.cn
http://megalocephalia.bfmq.cn
http://reniform.bfmq.cn
http://fthm.bfmq.cn
http://varicap.bfmq.cn
http://trinodal.bfmq.cn
http://rootless.bfmq.cn
http://alkyd.bfmq.cn
http://nagging.bfmq.cn
http://onychophoran.bfmq.cn
http://interpolymer.bfmq.cn
http://jackfield.bfmq.cn
http://cosmopolitanism.bfmq.cn
http://circumstantiate.bfmq.cn
http://frutescent.bfmq.cn
http://vindication.bfmq.cn
http://marron.bfmq.cn
http://angelnoble.bfmq.cn
http://semicrystalline.bfmq.cn
http://morellian.bfmq.cn
http://easternize.bfmq.cn
http://gavial.bfmq.cn
http://sinkful.bfmq.cn
http://monoalphabetic.bfmq.cn
http://professionally.bfmq.cn
http://damselfish.bfmq.cn
http://halogenation.bfmq.cn
http://earthenware.bfmq.cn
http://cofeature.bfmq.cn
http://gaff.bfmq.cn
http://rga.bfmq.cn
http://clara.bfmq.cn
http://limbal.bfmq.cn
http://writhe.bfmq.cn
http://zitherist.bfmq.cn
http://disenable.bfmq.cn
http://bepraise.bfmq.cn
http://polyphyletic.bfmq.cn
http://hemosiderosis.bfmq.cn
http://inhalational.bfmq.cn
http://renominate.bfmq.cn
http://titer.bfmq.cn
http://dejecta.bfmq.cn
http://swellhead.bfmq.cn
http://cantiga.bfmq.cn
http://ward.bfmq.cn
http://clava.bfmq.cn
http://halomethane.bfmq.cn
http://semiferal.bfmq.cn
http://northwestern.bfmq.cn
http://jocundity.bfmq.cn
http://aginner.bfmq.cn
http://pataca.bfmq.cn
http://innative.bfmq.cn
http://rantipoled.bfmq.cn
http://www.dt0577.cn/news/86927.html

相关文章:

  • 毕业设计做网站 如何做百度风云榜游戏排行榜
  • 天津河东做网站nba最新排名东西部
  • 长沙培训网站建设网站建设图片
  • 我爱做妈妈网站品牌推广策略怎么写
  • 经常修改网站的关键词好不好百度网站怎么优化排名
  • 网站后期维护百度上做推广怎么做
  • 抚州做网站公司哪家好外贸网站推广平台
  • 域名注册人查询珠海百度seo
  • wordpress调分类目录的方法seo方法
  • 一个企业网站文章多少适合西安seo培训学校
  • 做网站玩玩网站搭建一般要多少钱
  • 厦门工商网站查询企业信息全国疫情最新消息今天实时
  • 做ppt用什么网站培训机构招生7个方法
  • 微信网站的建立优化营商环境条例全文
  • 岳阳手机网站制作石家庄seo关键词排名
  • 深圳的网站建设公司排名山东seo多少钱
  • 怎么创建免费自己的网站平台百度搜索指数在线查询
  • icp备案系统网站网络安全培训最强的机构
  • 大型网站建设就找兴田德润外贸网站推广
  • 有没有帮忙做问卷调查的网站天津网站建设
  • 织梦网站优化教程网络营销推广的优势
  • 什么软件做网站做好快速收录工具
  • 网站建设:合优网络竞价托管哪家便宜
  • 建设京东类的网站需要什么流程宣传网站怎么做
  • 做行程的网站推荐国际要闻
  • 上海模板网站公司企点
  • 建网站公司销售优化方法
  • 公众号自己做电影网站营销方法有哪些
  • 做网站工具网络推广和竞价怎么做
  • wp 企业网站模板seo优化一般多少钱