一、简介

在前面的第二篇博文中对组件的生命周期虽然做了一个大略介绍,但总感觉说的过于简单,毕竟生命周期是React组件的核心部分。在我们熟练使用React挂载和合成组件来创建应用表现层的过程中,针对数据异步或延时问题,只有充分利用组件的生命周期来把握框架载入和数据处理的时机,才能将组件性能发挥到合理水平,并提高应用程序的健壮性。基本来说,组件的生命周期可分为挂载生命周期和更新生命周期两大部分,它们都包括一系列方法,这些方法在组件渲染前后会被触发,事实上,render方法本身也是组件生命周期的一部分。当然,根据用户使用的是ES6类创建组件还是React.createClass创建组件,它们体现的生命周期有一点点的区别。使用React.createClass创建组件时,开发者可以默认在getDefalutProps和getInitialState函数中分别初始化属性和state,而使用ES6类创建组件时,这两个函数被取而代之是constructor构造函数,在构造函数内部可以获取默认属性并且设置state。完整的生命周期对比如图所示:

二、详解

1、挂载生命周期

包括方法有:constructor/getDefault/getInitialState、componentWillMount、render、componentDidMount、componentWillUnmout。

2、更新生命周期

包括方法有:componentWillReceiveProps、shouldComponentUpdate、componentWillUpdate、componentDidUpdate。

三、示例

1、React.createClass

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Hello React</title>
<script src="react/react.js"></script>
<script src="react/react-dom.js"></script>
<script src="react/browser.min.js"></script>
</head>
<body>
<div id="container"></div>
<script type="text/babel"> const targetNode = document.getElementById("container"); const Component = React.createClass({ getDefaultProps(){
console.log("---getDefaultProps---");
return {}
}, getInitialState(){
console.log("---getInitialState---");
return ({
count: 0
})
}, componentWillMount(){
console.log("---componentWillMount---")
}, render(){
console.log("---render---");
return (
<div>
<h1>{`${this.props.title} ${this.state.count}`}</h1>
</div>
)
}, componentDidMount(){
console.log("---componentDidMount---");
this.setProps({
title:"I AM XYQ! Welcome me, current count is"
})
},
/*
* 现象:父组件更新子组件的props,在子组件接收到新的props时,更新子组件的state,但是却没有重新渲染。
* 原因:官方说在该函数中调用 this.setState() 将不会引起第二次渲染。每次子组件接收到新的props,都会重新渲染一次,
* 除非你做了处理来阻止(比如使用:shouldComponentUpdate)。 但是你可以在这次渲染前,根据新的props更新state,
* 更新state也会触发一次重新渲染,但react基于性能考虑,只会渲染一次。
* */
componentWillReceiveProps(nextProps){
console.log("---componentWillReceiveProps---");
this.setState({
count: this.state.count + 1
})
}, shouldComponentUpdate(nextProps, nextState){
console.log("---shouldComponentUpdate---"+`count is ${nextState.count}`);
return true
}, componentWillUpdate(nextProps, nextState){
console.log("---componentWillUpdate---")
}, componentDidUpdate(nextProps, nextState){
console.log("---componentDidUpdate---");
ReactDOM.unmountComponentAtNode(targetNode); //卸载根组件
}, componentWillUnmount(){
console.log("---componentWillUnmout---");
} }); ReactDOM.render(
<Component/>,
targetNode
) </script>
</body>
</html>

结果与分析  【需要把ReactDOM.unmountComponentAtNode()方法注释掉,才会显示结果,不然组件就从DOM上卸载了】

2、ES6

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Hello React</title>
<script src="react/react.js"></script>
<script src="react/react-dom.js"></script>
<script src="react/browser.min.js"></script>
</head>
<body>
<div id="container"></div>
<script type="text/babel"> const targetNode = document.getElementById("container"); //父组件
class Parent extends React.Component { constructor(props){
super(props)
this.state = {title:""}
this.deleteComponent = this.deleteComponent.bind(this)
}; deleteComponent(){
ReactDOM.unmountComponentAtNode(targetNode); //卸载根组件
} render(){
return (
<div onClick={this.deleteComponent}>
<Children name={this.state.title}/>
</div>)
}; //父组件修改传递给子组件的属性值,子组件会触发componentWillReceiveProps函数
componentDidMount(){
this.setState({
title: "I AM XYQ! Welcome me, current count is"
})
};
} //子组件
class Children extends React.Component{ constructor(props){
super(props);
this.state = {count:0};
console.log("---constructor---")
}; componentWillMount(){
console.log("---componentWillMount---")
}; render(){
console.log("---render---");
return (
<h1>{`${this.props.name} ${this.state.count}`}</h1>
)
}; componentDidMount(){
console.log("---componentDidMount---");
}; //此处获取的nextProps就是父组件的state中的title属性
componentWillReceiveProps(nextProps){
console.log("---componentWillReceiveProps---");
this.setState({
count: this.state.count + 1
})
}; shouldComponentUpdate(nextProps, nextState){
console.log("---shouldComponentUpdate---"+`count is ${nextState.count}`);
return true
}; componentWillUpdate(nextProps, nextState){
console.log("---componentWillUpdate---")
}; componentDidUpdate(nextProps, nextState){
console.log("---componentDidUpdate---");
}; componentWillUnmount(){
console.log("---componentWillUnmout---");
}
} ReactDOM.render(
<Parent/>,
targetNode
) </script>
</body>
</html>

结果与分析  【点击deleteComponent()事件,组件就从DOM上卸载了】

React: React组件的生命周期的更多相关文章

  1. reactjs入门到实战(七)---- React的组件的生命周期

    React的组件的生命周期有三个状态分别是:挂载(生产组件示例化.准备挂载到页面.挂载到页面).更新(更新值.更新DOM).和卸载(卸载后). >>>其他     getInitia ...

  2. react native组件的生命周期

    react native组件的生命周期 一.当页面第一次加载时,会依次调用: constructor() componentWillMount(): 这个函数调用时机是在组件创建,并初始化了状态之后, ...

  3. Android React Native组件的生命周期及回调函数

    熟悉android的童鞋应该都清楚,android是有生命周期的,其很多组件也是有生命周期.今天小编和大家分享的React Native组件的生命周期,还不了解的童鞋,赶紧来围观吧 在android开 ...

  4. 【RN - 基础】之React Native组件的生命周期

    下图描述了React Native中组件的生命周期: 从上图中可以看到,React Native组件的生命周期可以分为初始化阶段.存在阶段和销毁阶段. 实例化阶段 实例化阶段是React Native ...

  5. React Native组件、生命周期及属性传值props详解

    创建组件的三种方式 第一种:通过ES6的方式创建 /** * 方式一 :ES6 */ export default class HelloComponent extends Component { r ...

  6. React:组件的生命周期

    在组件的整个生命周期中,随着该组件的props或者state发生改变,其DOM表现也会有相应的变化.一个组件就是一个状态机,对于特定地输入,它总返回一致的输出. 一个React组件的生命周期分为三个部 ...

  7. react教程 — 组件的生命周期 和 执行顺序

    一.组件执行的生命周期:                  参考  https://www.cnblogs.com/soyxiaobi/p/9559117.html  或  https://www.c ...

  8. React(三)组件的生命周期

    Component Specs and LifeCycle <div id="app"></div> <script src="bower_ ...

  9. React Native——组件的生命周期

    组件生命周期 上流程图描述了组件从创建.运行到销毁的整个过程,可以看到如果一个组件在被创建,从开始一直到运行会依次调用getDefaultProps到render这五个函数:在运行过程中,如果有属性和 ...

  10. React入门--------组件的生命周期

    Mounting/组件挂载相关: componentWillMount componentDidMount Updating/组件更新相关: componentWillReceiveProps sho ...

随机推荐

  1. 【前端知乎系列】ArrayBuffer 和 Blob 对象

    本文首发在 个人博客 更多丰富的前端学习资料,可以查看我的 Github: <Leo-JavaScript>,内容涵盖数据结构与算法.HTTP.Hybrid.面试题.React.Angul ...

  2. vue中使用this遇到的坑

    在两个页面中创建函数,并且调用一个函数中能够获取到代表vue实例的this,而另一个却获取不到 页面1: <button id="login" v-text="$t ...

  3. HA-高可用集群

    原理:两台web服务器,通过心跳线进行通信,当主节点出现服务异常,备用节点通过探测判断主节点是否存活,若是不存活,就把服务接管过来. Web1和Web2中间有一根心跳线,检查对方的存活状态.流动IP: ...

  4. Redis-API

    Redis-API 简介 Redis 是一个基于内存的高效的键值行非关系型数据库,存取效率极高. python提供了两个类:分别为Redis和StrictRedis来实现Redis的命令操作.Redi ...

  5. Python 分支、循环、条件与枚举

    单行注释:# 注释内容多行注释:''' 注释内容 '''   Python 中有三种控制流语句: if for while 注:Python 中没有 Switch 这种开关语句 if 语句检测条件真, ...

  6. 在Mac上Python多版本切换

    1.安装Homebrewhttps://brew.sh/index_zh-cn.html 2.通过brew安装pyenv1)命令行输入:$ brew install pyenv(如果一直卡在Updat ...

  7. 如何快速将百度大脑AI技术内置智能小程序中

    实现效果: 该AI智能小程序目前集成了百度AI开放平台数十个AI服务产品功能,包括人脸识别.文字识别.表格识别.红酒识别.货币识别.地标识别.手势识别.商标识别.果蔬识别.菜品识别等图片识别功能,以及 ...

  8. opencv图像倾斜校正和切边

    #include<opencv2/opencv.hpp> #include<iostream> #include<cmath> using namespace st ...

  9. weed3-1.hello world

    Weed3 一个微型ORM框架(只有0.1Mb哦) 源码:https://github.com/noear/weed3 源码:https://gitee.com/noear/weed3 05年的时候开 ...

  10. AES加密原理和AOE工程实践

    在AI业务的开发的过程中,我们常常需要对模型文件进行加密.我们从以下几个方面来说一说AES的加密原理以及AOE里的工程实践. 常见的加密算法 AOE对模型加密需求的思考 AES的加密原理 AOE工程实 ...