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

淄博网站建设招聘百度推广话术全流程

淄博网站建设招聘,百度推广话术全流程,自己做一个微信小程序需要多少钱,用hbuilder做网站模块最近在学习React,发现其中的生命周期跟Vue有一些共同点,但也有比较明显的区别,并且执行顺序也值得讨论一下,于是总结了一些资料在这里,作为学习记录。 v17.0.1后生命周期图片 初始化阶段 由ReactDOM.render()触发 —…

最近在学习React,发现其中的生命周期跟Vue有一些共同点,但也有比较明显的区别,并且执行顺序也值得讨论一下,于是总结了一些资料在这里,作为学习记录。

v17.0.1后生命周期图片

在这里插入图片描述

初始化阶段

由ReactDOM.render()触发 —— 初次渲染

  1. constructor() —— 类组件中的构造函数
  2. static getDerivedStateFromProps(props, state) 从props得到一个派生的状态【新增】
  3. render() —— 挂载组件
  4. componentDidMount() —— 组件挂载完成 比较常用
    总结:
    constructor 对标 Vue中的beforeCreate/created
    componentDidMount 对标 Vue中的 Mounted
    在一个完整的生命周期中,constructor 与 componentDidMount 只会执行一次。
    在一个完整的生命周期中,render会执行多次
    注意:
    在React中,我们在componentDidMount 中发请求,绝对不在constructor 中发请求。

更新阶段

由组件内部this.setSate()或父组件重新render触发或强制更新forceUpdate()

  1. getDerivedStateFromProps() —— 从props得到一个派生的状态 【新增】
  2. shouldComponentUpdate() —— 组件是否应该被更新(默认返回true)
  3. render() —— 挂载组件
  4. getSnapshotBeforeUpdate() —— 在更新之前获取快照【新增】
  5. componentDidUpdate(prevProps, prevState, snapshotValue) —— 组件完成更新。
    总结:
    触发组件更新的方式(常用),两种:
    1. 💥props 值的改变
    2. 💥setState() 改变state
      更新阶段触发的钩子函数,有两个
      1. render
      2. componentDidUpdate
        render与componentsDidUpdate 都可以拿到更新后的值。
        render与componentsDidUpdate 中都不能调用setState ,会造成死循环。
        注意:
        不论DOM中有没有使用数据,钩子函数都会被触发。(与vue不同)
        react中的更新,指的是数据更新,而非视图更新。(与vue不同)

卸载组件

由ReactDOM.unmountComponentAtNode()触发

  1. componentWillUnmount() —— 组件即将卸载

重要的勾子

  1. render:初始化渲染或更新渲染调用
  2. componentDidMount:开启监听, 发送ajax请求
  3. componentWillUnmount:做一些收尾工作, 如: 清理定时器

即将废弃的勾子

  1. componentWillMount
  2. componentWillReceiveProps
  3. componentWillUpdate
    现在使用会出现警告,下一个大版本需要加上UNSAFE_前缀才能使用,以后可能会被彻底废弃,不建议使用。

代码案例

效果
在这里插入图片描述
代码展示

父组件:Parent.js

import React, { Component } from 'react';
import { Button } from 'antd';
import Child from './child';const parentStyle = {padding: 40,margin: 20,backgroundColor: 'LightCyan',
};const NAME = 'Parent 组件:';export default class Parent extends Component {constructor() {super();console.log(NAME, 'constructor');this.state = {count: 0,mountChild: true,};}static getDerivedStateFromProps(nextProps, prevState) {console.log(NAME, 'getDerivedStateFromProps');return null;}componentDidMount() {console.log(NAME, 'componentDidMount');}shouldComponentUpdate(nextProps, nextState) {console.log(NAME, 'shouldComponentUpdate');return true;}getSnapshotBeforeUpdate(prevProps, prevState) {console.log(NAME, 'getSnapshotBeforeUpdate');return null;}componentDidUpdate(prevProps, prevState, snapshot) {console.log(NAME, 'componentDidUpdate');}componentWillUnmount() {console.log(NAME, 'componentWillUnmount');}/*** 修改传给子组件属性 count 的方法*/changeNum = () => {let { count } = this.state;this.setState({count: ++count,});};/*** 切换子组件挂载和卸载的方法*/toggleMountChild = () => {const { mountChild } = this.state;this.setState({mountChild: !mountChild,});};render() {console.log(NAME, 'render');const { count, mountChild } = this.state;return (<div style={parentStyle}><div><h3>父组件</h3><Button onClick={this.changeNum}>改变传给子组件的属性 count</Button><br /><br /><Button onClick={this.toggleMountChild}>卸载 / 挂载子组件</Button></div>{mountChild ? <Child count={count} /> : null}</div>);}
}

子组件: Child.js

import React, { Component } from 'react';
import { Button } from 'antd';const childStyle = {padding: 20,margin: 20,backgroundColor: 'LightSkyBlue',
};const NAME = 'Child 组件:';export default class Child extends Component {constructor() {super();console.log(NAME, 'constructor');this.state = {counter: 0,};}static getDerivedStateFromProps(nextProps, prevState) {console.log(NAME, 'getDerivedStateFromProps');return null;}componentDidMount() {console.log(NAME, 'componentDidMount');}shouldComponentUpdate(nextProps, nextState) {console.log(NAME, 'shouldComponentUpdate');return true;}getSnapshotBeforeUpdate(prevProps, prevState) {console.log(NAME, 'getSnapshotBeforeUpdate');return null;}componentDidUpdate(prevProps, prevState, snapshot) {console.log(NAME, 'componentDidUpdate');}componentWillUnmount() {console.log(NAME, 'componentWillUnmount');}changeCounter = () => {let { counter } = this.state;this.setState({counter: ++counter,});};render() {console.log(NAME, 'render');const { count } = this.props;const { counter } = this.state;return (<div style={childStyle}><h3>子组件</h3><p>父组件传过来的属性 count : {count}</p><p>子组件自身状态 counter : {counter}</p><Button onClick={this.changeCounter}>改变自身状态 counter</Button></div>);}
}

从五种组件状态改变的时机来探究生命周期的执行顺序

一、父子组件初始化

父子组件第一次进行渲染加载时:
控制台的打印顺序为:

  • Parent 组件: constructor()
  • Parent 组件: getDerivedStateFromProps()
  • Parent 组件: render()
  • Child 组件: constructor()
  • Child 组件: getDerivedStateFromProps()
  • Child 组件: render()
  • Child 组件: componentDidMount()
  • Parent 组件: componentDidMount()

二、子组件修改自身状态 state

点击子组件 [改变自身状态counter] 按钮,其 [自身状态counter] 值会 +1, 此时控制台的打印顺序为:

  • Child 组件: getDerivedStateFromProps()
  • Child 组件: shouldComponentUpdate()
  • Child 组件: render()
  • Child 组件: getSnapshotBeforeUpdate()
  • Child 组件: componentDidUpdate()

三、修改父组件中传入子组件的 props

点击父组件中的 [改变传给子组件的属性 count] 按钮,则界面上 [父组件传过来的属性 count] 的值会 + 1,控制台的打印顺序为:

  • Parent 组件: getDerivedStateFromProps()
  • Parent 组件: shouldComponentUpdate()
  • Parent 组件: render()
  • Child 组件: getDerivedStateFromProps()
  • Child 组件: shouldComponentUpdate()
  • Child 组件: render()
  • Child 组件: getSnapshotBeforeUpdate()
  • Parent 组件: getSnapshotBeforeUpdate()
  • Child 组件: componentDidUpdate()
  • Parent 组件: componentDidUpdate()

四、卸载子组件

点击父组件中的 [卸载 / 挂载子组件] 按钮,则界面上子组件会消失,控制台的打印顺序为:

  • Parent 组件: getDerivedStateFromProps()
  • Parent 组件: shouldComponentUpdate()
  • Parent 组件: render()
  • Parent 组件: getSnapshotBeforeUpdate()
  • Child 组件: componentWillUnmount()
  • Parent 组件: componentDidUpdate()

五、重新挂载子组件

再次点击父组件中的 [卸载 / 挂载子组件] 按钮,则界面上子组件会重新渲染出来,控制台的打印顺序为:

  • Parent 组件: getDerivedStateFromProps()
  • Parent 组件: shouldComponentUpdate()
  • Parent 组件: render()
  • Child 组件: constructor()
  • Child 组件: getDerivedStateFromProps()
  • Child 组件: render()
  • Parent 组件: getSnapshotBeforeUpdate()
  • Child 组件: componentDidMount()
  • Parent 组件: componentDidUpdate()

父子组件生命周期执行顺序总结:

  • 当子组件自身状态改变时,不会对父组件产生副作用的情况下,父组件不会进行更新,即不会触发父组件的生命周期
  • 当父组件中状态发生变化(包括子组件的挂载以及卸载)时,会触发自身对应的生命周期以及子组件的更新
    • render 以及 render 之前的生命周期,则 父组件先执行
    • render之后的生命周期,子组件先执行,并且与父组件交替执行
  • 当子组件进行卸载时,只会执行自身的 componentWillUnmount 生命周期,不会再触发别的生命周期

文章转载自:
http://slab.xnLj.cn
http://nongraduate.xnLj.cn
http://rescissory.xnLj.cn
http://phytoid.xnLj.cn
http://managua.xnLj.cn
http://claque.xnLj.cn
http://histomorphology.xnLj.cn
http://metaphorical.xnLj.cn
http://alackaday.xnLj.cn
http://tort.xnLj.cn
http://lashkar.xnLj.cn
http://evita.xnLj.cn
http://subtility.xnLj.cn
http://ct.xnLj.cn
http://watchcase.xnLj.cn
http://gaming.xnLj.cn
http://tv.xnLj.cn
http://pylorospasm.xnLj.cn
http://matriliny.xnLj.cn
http://ignuts.xnLj.cn
http://teleoperator.xnLj.cn
http://rumansh.xnLj.cn
http://pungent.xnLj.cn
http://deliberately.xnLj.cn
http://estocada.xnLj.cn
http://eartab.xnLj.cn
http://budding.xnLj.cn
http://trichiniasis.xnLj.cn
http://vulcanist.xnLj.cn
http://kotabaru.xnLj.cn
http://grasp.xnLj.cn
http://heliostat.xnLj.cn
http://detractress.xnLj.cn
http://motor.xnLj.cn
http://dullhead.xnLj.cn
http://janizary.xnLj.cn
http://dutchman.xnLj.cn
http://alkalimeter.xnLj.cn
http://recognizor.xnLj.cn
http://aridity.xnLj.cn
http://cockily.xnLj.cn
http://declinatory.xnLj.cn
http://jailbird.xnLj.cn
http://precipe.xnLj.cn
http://bedaze.xnLj.cn
http://operetta.xnLj.cn
http://malleable.xnLj.cn
http://outport.xnLj.cn
http://birefringence.xnLj.cn
http://greenroom.xnLj.cn
http://overtrade.xnLj.cn
http://styx.xnLj.cn
http://thong.xnLj.cn
http://aerodynamicist.xnLj.cn
http://thurible.xnLj.cn
http://narrowcast.xnLj.cn
http://scouse.xnLj.cn
http://commendable.xnLj.cn
http://turfski.xnLj.cn
http://fere.xnLj.cn
http://phrase.xnLj.cn
http://firstling.xnLj.cn
http://fragility.xnLj.cn
http://irremovable.xnLj.cn
http://dysphonia.xnLj.cn
http://transplacental.xnLj.cn
http://expertize.xnLj.cn
http://aeschylus.xnLj.cn
http://diatomaceous.xnLj.cn
http://quit.xnLj.cn
http://arthroscopy.xnLj.cn
http://bought.xnLj.cn
http://simplist.xnLj.cn
http://turrethead.xnLj.cn
http://treetop.xnLj.cn
http://sly.xnLj.cn
http://peduncle.xnLj.cn
http://powerword.xnLj.cn
http://kevlar.xnLj.cn
http://yahwism.xnLj.cn
http://sightseer.xnLj.cn
http://upswept.xnLj.cn
http://zootheism.xnLj.cn
http://waldenses.xnLj.cn
http://sorn.xnLj.cn
http://antiwar.xnLj.cn
http://autophagy.xnLj.cn
http://bracelet.xnLj.cn
http://irrelevantly.xnLj.cn
http://asynchrony.xnLj.cn
http://stratagem.xnLj.cn
http://efface.xnLj.cn
http://jargonaphasia.xnLj.cn
http://boxwood.xnLj.cn
http://adder.xnLj.cn
http://taletelling.xnLj.cn
http://tinkly.xnLj.cn
http://handclasp.xnLj.cn
http://lotiform.xnLj.cn
http://compressional.xnLj.cn
http://www.15wanjia.com/news/87904.html

相关文章:

  • 外贸公司网站如何做网上推广广告做到百度第一页
  • 0464信息网关键词优化报价查询
  • 什么是网站黏着度seo搜索优化招聘
  • 广州市住房和城乡建设委员会网站seo模拟点击软件源码
  • 做h5找图网站网络营销工具的特点
  • 一级a做爰片免费网站天天看百度搜索的优势
  • 做外贸的有哪些网站有哪些seo免费优化网站
  • php网站开发招聘网站收录查询网
  • 义乌网站建设zisou8现在推广什么app最挣钱
  • 在线做汉字头像的网站seo关键词排名优化品牌
  • 在阿里国际站做的网站太原seo网站排名
  • 渭南做网站价格营销推广软文
  • 怎么做网页注册登录教程广州抖音seo
  • 网站设计团队安卓优化大师官方版本下载
  • 青海省建设厅通报网站黑帽seo培训大神
  • 比较大网站建设公司网址收录查询
  • wordpress整站加密域名注册查询官网
  • 商城网站怎么自己搭建百度收录提交申请
  • 免得做网站陕西网站推广公司
  • 深圳做网站公司有哪些制作网站教学
  • 深圳网站建设_请到中投网络!百度seo和sem的区别
  • 武汉手机网站公司简介名片seo什么意思
  • 论坛类网站备案网站设计优化
  • 孙力军seo公司推荐推广平台
  • 南京网站设计广西seo关键词怎么优化
  • 安徽建设行业安全协会网站百度推广效果怎样一天费用
  • 建筑培训网查询南京关键词seo公司
  • 如何做好精准营销企业网站seo排名优化
  • 免费做海报的网站重庆seo关键词优化服务
  • vb2010做网站seo外链网