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应用的最小单位. 元素是普通的对象. 元素是构成 ...
随机推荐
- 关于模型的合法性,Entity.IsValid()合理吗?
关于模型的合法性,Entity.IsValid()合理吗? 背景 见过很多框架(包括我自己的)都会在实体的定义中包含一个IsValid()方法,用来判断实体的合法性,是否应该这样设计呢?本文就这个问题 ...
- 使用SmsManager服务群发短信
SmsManager是Android提供的一个非常常见的服务,SmsManager提供了一系列sendXxxMessage()方法用于发送短信,不过短信通常都是普通文本,调用sendTextMessa ...
- Fixjs实践——标签、按钮控件
Fixjs介绍 Fixjs是一款javascript界面基础框架,主要为开发复杂组件提供底层的框架支持. Fixjs 0.3.0主要增加了文本显示类fixjs.text.TextField 支持的主要 ...
- NodeJs+Express实现简单的Web增删改查
前一段时间,公司组织了一次NodeJs的技术分享,自己有幸去听了听,第一次接触NodeJs,后来经过自己学习和探索,完成了一个很简单的Web演示项目,在这里和初学者做以分享,开发工具:WebStorm ...
- zookeeper删除kafka元数据
问题:卸载kafka前未删除kafka topic,重新安装kafka后,生成跟之前topic名字相同的topic时报错,显示topic已存在 [root@d96 ~]# kafka-topics - ...
- js实现表格的增删改查
这份代码实现了对表格的增加,删除,更改,查询. 点击一次添加按钮,表格会增加一行. 点击重置按钮,输入框的内容会被清空. 添加一行后,最后两格为更改和删除.点击更改,原有内容会各自显示在一个输入框内, ...
- CodeForces 749D Leaving Auction
二分查找,$set$. 对于某一次询问,如果把人删光了,那么输出$0$ $0$. 如果只剩下$1$个人,那么输出那个人喊的最低价格. 如果剩下的人数有大于等于两个, 这时最底下出现的情景必然是红色部分 ...
- 修改linux默认ssh 端口
修改ssh的默认端口 1.编辑ssh配置文件: #vi /etc/ssh/ssh_config #vi /etc/ssh/sshd_config ...
- AngularCSS--关于angularjs动态加载css文件的方法(仅供参考)
AngularCSS CSS on-demand for AngularJS Optimize the presentation layer of your single-page apps by d ...
- 7 -- Spring的基本用法 -- 10...
7.10 高级依赖关系配置 组件与组件之间的耦合,采用依赖注入管理:但基本类型的成员变量值,应直接在代码中设置. Spring支持将任意方法的返回值.类或对象的Field值.其他Bean的getter ...