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应用的最小单位. 元素是普通的对象. 元素是构成 ...
随机推荐
- T-SQL编程的基本语法和思想
通过一个很实用的例子让你学会T-SQL编程的基本语法和思想 例子需求:把Execl中的三级分类(列别的三级联动)数据导入到数据库中. Excel表中数据的显示格式: 图1 数据库中表的显示格式: ...
- 解决TXT乱码问题
初装Ubuntu,打开windows保存的txt文件很可能会遇到各种乱码问题. 下面是wiki ubuntu里的解决办法: Gedit中文乱码 缺省配置下,用 Ubuntu 的文本编辑器(gedit) ...
- iOS 测试驱动开发
测试驱动开发是敏捷开发的一部分,它一般有“red-green- refactor”步骤 iOS测试驱动开发的工具 一. OCUnit 是Xcode自带的测试工具 其使用步骤分为 1 建立测试的Targ ...
- c/c++ 算法之快速排序法 冒泡排序法,选择排序法,插入排序法
本文详细叙述和实现了快速排序算法,冒泡排序 选择排序 插入排序比较简单,原理在这里不再详述,直接用代码进行了实现. 快速排序法(quicksort)是目前所公认最快的排序方法之一(视解题的对象而定), ...
- Linux历史上线程的3种实现模型
一.概述 这里以Linux为例.Linux历史上,最开始使用的线程是LinuxThreads,但Li ...
- vsftpd.conf 详解与实例配置
#################匿名权限控制############### anonymous_enable=YES #是否启用匿名用户 no_anon_password=YES #匿名用 ...
- 使用redis缓存数据需要注意的问题以及个人的一些思考和理解
之前我有博客也尝试过使用redis,在实际的项目中确实作用挺大的.至少对于数据的频繁读取来说都起着至关重要的作用. 但是随着技术的学习,慢慢的业务要复杂起来,以后也许会用到redis集群,所以在这边查 ...
- (十一)if...else&for循环&while循环
----------------------------------if else------------------------------1.最基本的if语句:if name =="Al ...
- SSH 一些错误的解决办法
1.主动访问的机器需要创建私钥和公钥 (client) #cd ~#mkdir .ssh#chmod 700 .ssh#cd .ssh#ssh-keygen -t rsa //一路回车,各种提示按默认 ...
- 微信小程序开发(1)
底限,HTML,CSS,JS得会 先过一下官方的文档:https://mp.weixin.qq.com/debug/wxadoc/introduction/index.html?t=20161230 ...