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++中Object所占内存空间探索1
关于C++中Object所占内存空间探索(一) 有如下问题: 1. 一个空类, class X{ }; 2.类中含有数据成员(Data Member), class X { public: //Fun ...
- .Net 中的反射机制
.Net 中的反射机制 概述反射 通过反射可以提供类型信息,从而使得我们开发人员在运行时能够利用这些信息构造和使用对象. 反射机制允许程序在执行过程中动态地添加各种功能. 运行时类型标识 运行时类型标 ...
- ADO读取EXCEL
窗体上拖放ADOQuery1,DataSetProvider1,DataSource1,ClientDataSet1,OpenDialog1, ExcelApplication1,ExcelWorkb ...
- C++malloc,calloc,realloc,free函数
在进行C/c++编程的时候,需要程序员对内存的了解比较清楚,经常需要操作的内存可分为下面几个类别: 1.堆栈区(stack):由编译器自动分配与释放,存放函数的参数值,局部变量,临时变量等等, ...
- [置顶] 使用Android OpenGL ES 2.0绘图之五:添加运动
传送门 ☞ 系统架构设计 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229 传送门 ☞ GoF23种设计模式 ☞ 转载请注明 ☞ http://blog.csd ...
- Java核心技术 卷Ⅰ 基础知识(1)
第三章 Java的基本程序设计结构 Java对大小写敏感 命名规范为骆驼命名法,不能使用保留字 main方法必须声明为public 如果main方法正常退出,java应用程序的退出代码为0,表示成功运 ...
- xhtml头文件设置
设置字符集编码: <head> <meta http-equiv="Content-Type" Content="text/html" cha ...
- web页面在微信里打开,字体颜色不正常显示
问题:写的web项目在微信里的webview里打开(iphone手机),会出现颜色的不识别.写的是白色,数字的部分会过了3-5秒后,变成黑色! 原因:在iphone手机里,数字的部分(具体的长度没有测 ...
- url截取
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- List去重
因为用到list,要去除重复数据,尝试了几种方法.记录于此... 测试数据: List<string> li1 = new List<string> { "8&quo ...