react学习笔记-05 lifecycle
根据React官网,react有三个生命状态:装载(Mounting),更新(updating),卸载()
一:装载
装载:componentWillMount/componentDidMount(组件即将被装载、组件已经被装载)
export default class blog extends React.Component{
constructor(props){
super(props);
this.state = {count:0};
};
componentWillMount(){
console.log("will mount");
};
componentDidMount(){
console.log("did mount");
console.log(ReactDom.findDOMNode(this));
};
render(){
console.log("render");
return (
<div>hello world! {this.state.count}</div>
);
}
}
输出结果:

可以看出,在componentWillMount里面进行setState,组件不会重新渲染.如果在componentDidMount里面setState,组件会重新渲染。
在render结束之后,就可以获得DOM 节点了。
在componentDidMount通常做一些ajax或者settimeout/setInterval操作,再更新DOM进行update操作
二:卸载
componentWillunMount:取消事件监听,清除定时器
export default class blog extends React.Component{
constructor(props){
super(props);
this.state = {count:0};
};
componentWillMount(){
console.log("will mount");
this.setState({
count:3
});
};
componentDidMount(){
console.log("did mount");
console.log(ReactDom.findDOMNode(this));
};
componentWillUnmount(){
console.log("you want kill me?");
};
killMyself(){
ReactDom.unmountComponentAtNode(document.getElementById('blog'));
console.log("yes, I want to kill you");
};
render(){
console.log("render");
return (
<div>hello world! {this.state.count}
<button onClick = {this.killMyself} value="kill"></button>
</div>
);
}
}

如果在componentWillMount里面使用setInterval:
componentWillMount(){
console.log("will mount");
var self = this;
this.timer = setInterval(function(){
self.setState({count:self.state.count+1});
},1000);
};
点击清空按钮后会出现下面warning:

该warning的出现是因为DOM被清除了,但是计时器还在。所以这个时候可以在componentWillUnmount里面清空setInterval
componentWillUnmount(){
console.log("you want kill me?");
clearInterval(this.timer);
};
三:更新
第一次创建组件的时候,不会调用update相关的方法。
shouldComponentUpdate返回true时,其他的update方法才会被触发。
shouldComponentUpdate(nextProp,nextState){
//判断是否需要重新渲染
console.log("shouldComponentUpdate");
if(nextState >2){
return false;
}
return true;
};
componentWillUpdate(nextProps,nextState){
//做一些数据的处理
console.log("componentWillUpdate");
};
componentDidUpdate(){
//可以获取更新后的DOM结构
console.log("update success");
};
doUpdate(){
this.setState({count:this.state.count+1});
};
render(){
console.log("render");
return (
<div>hello world! {this.state.count}
<button onClick = {this.doUpdate.bind(this)}>update</button>
</div>
);
}

还有一个重要的update方法:
componentWillReceivePops
用props更新子组件的state,判断子组件是否更新
var SubMessage = React.createClass({
componentWillReceiveProps (nextProps){
console.log("子组件将要获得props");
},
shouldComponentUpdate (nextProps,nextState){
if(nextProps.count >4){
return false;
}
return true;
},
render(){
return (<h3>{this.props.count}</h3>);
}
});
export default class blog extends React.Component{
constructor(props){
super(props);
this.state = {count:0};
};
//更新周期
shouldComponentUpdate (nextProp,nextState){
//判断是否需要重新渲染
console.log("shouldComponentUpdate");
if(nextState.count >8){
return false;
}
return true;
};
componentWillUpdate(nextProps,nextState){
//做一些数据的处理
console.log("componentWillUpdate");
};
componentDidUpdate(){
//可以获取更新后的DOM结构
console.log("update success");
};
doUpdate(){
this.setState({count:this.state.count+1});
};
render(){
console.log("render");
return (
<div>hello world! {this.state.count}
<button onClick = {this.doUpdate.bind(this)}>update</button>
<SubMessage count={this.state.count}></SubMessage>
</div>
);
}
}

react学习笔记-05 lifecycle的更多相关文章
- React学习笔记--程序调试
React学习笔记 二 程序调试 前面我们搭建好了React的基本开发环境,可以编写基本的React js程序了.但完成的开发环境肯定包含调试器,怎么调试用React编写的JS程序呢?有浏览器,比 ...
- react学习笔记1--基础知识
什么是react A JAVASCRIPT LIBRARY FOR BUILDING USER INTERFACES[React是一个用于构建用户界面的JavaScript库.] React之所以快, ...
- React学习笔记(一)- 入门笔记
React入门指南 作者:狐狸家的鱼 本文链接:React学习笔记 GitHub:sueRimn 1.组件内部状态state的修改 修改组件的每个状态,组件的render()方法都会再次运行.这样就可 ...
- 机器学习实战(Machine Learning in Action)学习笔记————05.Logistic回归
机器学习实战(Machine Learning in Action)学习笔记————05.Logistic回归 关键字:Logistic回归.python.源码解析.测试作者:米仓山下时间:2018- ...
- React学习笔记(七)条件渲染
React学习笔记(七) 六.条件渲染 使用if或条件运算符来创建表示当前状态的元素. 可以使用变量来存储元素.比如: let button = null; if (isLoggedIn) { but ...
- React学习笔记(六)事件处理
React学习笔记(六) 五.事件处理 React事件绑定属性的命名采用驼峰写法,不同于传统DOM全部小写. 如果采用JSX的语法,事件函数需要用大括号{}包裹函数名,不同于传统DOM字符串小括号的方 ...
- React学习笔记(五)State&声明周期
React学习笔记(五) 四.State&声明周期 可以为组件添加"状态(state)".状态与属性相似,但是状态是私有的,完全受控于当前组件. 局部状态就是只能用于类(定 ...
- React学习笔记 - 组件&Props
React Learn Note 4 React学习笔记(四) 标签(空格分隔): React JavaScript 三.组件&Props 组件可以将UI切分成一些独立的.可复用的部件,这样你 ...
- React学习笔记 - 元素渲染
React Learn Note 3 React学习笔记(三) 标签(空格分隔): React JavaScript 二.元素渲染 元素是构成react应用的最小单位. 元素是普通的对象. 元素是构成 ...
随机推荐
- 从零开始学C++之虚继承和虚函数对C++对象内存模型造成的影响
首先重新回顾一下关于类/对象大小的计算原则: 类大小计算遵循结构体对齐原则 第一个数据成员放在offset为0的位置 其它成员对齐至min(sizeof(member),#pragma pack(n) ...
- cin 和scanf,scanf比cin快很多
//#include <iostream> #include <stdio.h> //#include <fstream> //using namespace st ...
- 深入浅出学习Spring框架(四):IoC和AOP的应用——事务配置
在前文 深入浅出学习Spring框架(一):通过Demo阐述IoC和DI的优势所在. 深入浅出学习Spring框架(三):AOP 详解 分别介绍了Spring的核心功能——IoC和AOP,光讲知识远远 ...
- .Net程序员学用Oracle系列(3):数据库编程规范
<.Net程序员学用Oracle系列:导航目录> 本文大纲 1.书写规范 1.1.大小写风格 1.2.缩进风格 1.3.换行 1.4.其它 2.命名规范 2.1.数据库对象命名 2.2.变 ...
- java 异常处理机制及说明。
又抄袭了一篇文章,其实就是想保存到自己的博客中而已,文章出处:http://www.cnblogs.com/LilianChen/p/4639471.html 1. 如何捕获异常 try { 可能会出 ...
- Array类型方法总结(未写完)
转换方法: toString() toLocaleString() valueOf() 栈方法: push()---在数组末尾添加项,返回修改后的长度, pop()---在数组末尾移除项,返 ...
- 【成长之路】【python】python基础1
1.python的优点 高级语言:不需考虑底层实现的细节 可移植性:python程序不需经过任何修改就可以在所有的平台系统上运行 可扩展性:可以把用c和c++实现的代码嵌到python中 可嵌入性:可 ...
- CentOS+OpenCV图像的读入、显示
以管理员身份运行su root输入密码 定位到自己的桌面目录 gedit 1.cpp 编辑内容 #include<opencv2/opencv.hpp>using namespace cv ...
- CoreJavaE10V1P3.8 第3章 Java的基本编程结构-3.8 控制流程(Control Flow)
通过使用条件语句.循环语句可以实现流程的控制. 3.8.1 块作用域(Block Scope) 块(Block)就是由一对花括号包围起来的部分.他指定了一个变量的生存范围,与一个方法的操作范围. Ja ...
- hdu1116回溯N皇后问题
题目连接 经过思考,不难发现:恰好N个皇后放在不同行不同列,那么是不是可以转换成N个皇后所在行分别确定(一人一行)的情况下对她们的所在列的枚举. 也就是列的全排列生成问题,我们用c[x]表示x行皇后的 ...