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应用的最小单位. 元素是普通的对象. 元素是构成 ...
随机推荐
- static类成员(变量和函数)
0. 使用背景 对于特定类类型的全体对象而言,访问一个全局对象有时是必要的.也许,在程序的任意点需要统计已创建的特定类类型对象的数量:或者,全局对象可能是指向类的错误处理例程的一个指针:或者,它是指向 ...
- jsp传值乱码解决办法
在jsp中,我们经常从数据库读取数据返回客户端,但我们常常在制作时出现乱码现象,所以我们可以用<%request.setCharacterEncoding("UTF-8"); ...
- POJ 1013 小水题 暴力模拟
Counterfeit Dollar Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35774 Accepted: 11 ...
- 基于Spark的用户行为路径分析
研究背景 互联网行业越来越重视自家客户的一些行为偏好了,无论是电商行业还是金融行业,基于用户行为可以做出很多东西,电商行业可以归纳出用户偏好为用户推荐商品,金融行业可以把用户行为作为反欺诈的一个点,本 ...
- 【汇编语言】DEBUG的使用
在masm for windows中,需要先生存exe文件,然后再点调试按钮. 常用的命令有: R命令:查看.改变CPU寄存器的内容:如果要修改某个寄存器的内容,可以在r的后面接上空格和寄存器名.如: ...
- js处理层级数据结构的一些总结
开发者对复杂的数据结构的处理能力也是体现开发者水平的一个度量吧...最近发现自己对一些嵌套数据结构.层级数据结构的处理能力不大足...经常被这些把自己绕晕...严重影响开发效率...就稍微低总结了一下 ...
- java基础练习 2
public class Second { /* * 打印出杨辉三角形(要求打印出10行如下图) */ public static void main(String[] args){ int i,j, ...
- 为什么需要DevOps
过去一年以来,一批来自欧美的.不墨守陈规的系统管理员和开发人员一直在谈论一个新概念:DevOps.DevOps 就是开发(Development) 和运维(Operations)这两个领域的合并.(如 ...
- Python高手之路【十】python基础之反射
反射说简单点 --> 就是利用字符串的形式去对象(模块)中操作(寻找/检查/删除/设置)成员. 需求:由用户输入一个模块名,用户输入什么模块名,文件中就导入什么模块: 1:文件都在同一目录下的导 ...
- url截取
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...