React(三)组件的生命周期
Component Specs and LifeCycle
<div id="app"></div>
<script src="bower_components/react/react.min.js"></script>
<script src="bower_components/react/react-dom.min.js"></script>
<script src="lib/babel-core/browser.min.js"></script>
<script type="text/babel">
var MessageBox = React.createClass({
getInitialState: function () {
return {
count: 0
}
},
componentWillMount: function () {
console.log('componentWillMount');
var self = this;
this.timer = setInterval(function () {
self.setState({
count: self.state.count + 1
});
}, 1000);
},
componentDidMount: function () {
console.log('componentDidMount');
console.log(this);
},
componentWillUnmount: function () {
console.log('卸载掉组件');
clearInterval(this.timer);
},
render: function () {
return (
<div>
<h1>{this.state.count}</h1>
</div>
);
}
});
var messagebox = ReactDOM.render(
<MessageBox/>,
document.getElementById('app')
);
</script>
<div id="app"></div>
<script src="bower_components/react/react.min.js"></script>
<script src="bower_components/react/react-dom.min.js"></script>
<script src="lib/babel-core/browser.min.js"></script>
<script type="text/babel">
var MessageBox = React.createClass({
getInitialState: function () {
return {
count: 0
}
},
getDefaultProps: function () {
},
/*componentWillMount: function () {
},
componentDidMount: function () {
},
componentWillUnmount: function () {
},
*/
shouldComponentUpdate: function (nextProp, nextState) {
console.log('shouldComponentUpdate');
if(nextState.count > 3) return false;
return true; // 必须返回一个true或者false
},
componentWillUpdate: function (nextProp, nextState) {
console.log('componentWillUpdate');
},
componentDidUpdate: function () {
console.log('成功更新啦');
},
doUpdate: function () {
this.setState({
count: this.state.count + 1
});
},
render: function () {
return (
<div>
<h1>{this.state.count}</h1>
<button onClick={this.doUpdate}>手动更新一下组件(包括子组件)</button>
<SubMessage message={this.state.count} />
</div>
);
}
});
var SubMessage = React.createClass({
componentWillReceiveProps: function (nextProp) {
console.log('子组件将要获得prop');
},
shouldComponentUpdate: function (nextProp, nextState) {
if(nextProp.message > 2) return false;
return true;
},
render: function () {
return (
<div>
<h3>{this.props.message}</h3>
</div>
);
}
});
var messagebox = ReactDOM.render(
<MessageBox/>,
document.getElementById('app')
);
</script>
React(三)组件的生命周期的更多相关文章
- reactjs入门到实战(七)---- React的组件的生命周期
React的组件的生命周期有三个状态分别是:挂载(生产组件示例化.准备挂载到页面.挂载到页面).更新(更新值.更新DOM).和卸载(卸载后). >>>其他 getInitia ...
- react native组件的生命周期
react native组件的生命周期 一.当页面第一次加载时,会依次调用: constructor() componentWillMount(): 这个函数调用时机是在组件创建,并初始化了状态之后, ...
- Android React Native组件的生命周期及回调函数
熟悉android的童鞋应该都清楚,android是有生命周期的,其很多组件也是有生命周期.今天小编和大家分享的React Native组件的生命周期,还不了解的童鞋,赶紧来围观吧 在android开 ...
- 【RN - 基础】之React Native组件的生命周期
下图描述了React Native中组件的生命周期: 从上图中可以看到,React Native组件的生命周期可以分为初始化阶段.存在阶段和销毁阶段. 实例化阶段 实例化阶段是React Native ...
- React Native组件、生命周期及属性传值props详解
创建组件的三种方式 第一种:通过ES6的方式创建 /** * 方式一 :ES6 */ export default class HelloComponent extends Component { r ...
- React:组件的生命周期
在组件的整个生命周期中,随着该组件的props或者state发生改变,其DOM表现也会有相应的变化.一个组件就是一个状态机,对于特定地输入,它总返回一致的输出. 一个React组件的生命周期分为三个部 ...
- react教程 — 组件的生命周期 和 执行顺序
一.组件执行的生命周期: 参考 https://www.cnblogs.com/soyxiaobi/p/9559117.html 或 https://www.c ...
- React Native——组件的生命周期
组件生命周期 上流程图描述了组件从创建.运行到销毁的整个过程,可以看到如果一个组件在被创建,从开始一直到运行会依次调用getDefaultProps到render这五个函数:在运行过程中,如果有属性和 ...
- React入门--------组件的生命周期
Mounting/组件挂载相关: componentWillMount componentDidMount Updating/组件更新相关: componentWillReceiveProps sho ...
随机推荐
- js选择器
原生JS选择器有getElementById.getElementsByName.getElementsByTagName和getElementsByClassName这四个,下面我就一个一个介绍这四 ...
- 1、在MAC上搭建React Native开发环境
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- Codeforces #380 div2 E(729E) Subordinates
E. Subordinates time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- 百度音乐api
百度音乐全接口 会利用使用接口找歌简单又快捷 http://tingapi.ting.baidu.com/v1/restserver/ting 获取方式:GET 参数:format=json或xml& ...
- 对bit、byte、TByte、Char、string、进制的认识
在学校老师就教1byte = 8bit,一个Byte在内存中占8个房间.每个房间都有门牌号.找到内存中的内容就找门牌号,寻址什么的,虽然在听,但是脑袋里一头雾水,到现在只知道会用就行,但原理也不是那么 ...
- 如何去掉底部的织梦版权信息powered by dedecms
由于织梦DEDECMS程序6月份的漏洞,很多织梦网站都被黑了,所以大家都在抓紧时间更新系统补丁.但是这次的DEDECMS V5.7版本更新后,在前台网页底部会出现织梦版权信息 “powered by ...
- android 编译代码注意事项
1 安装openjdk1.7 sudo add-apt-repository ppa:openjdk-r/ppa sudo apt-apt update sudo apt-get install op ...
- java-android推送
之前做的推送,考虑了很多,最后因为各个因素,选择了极光的.
- sql查询重复记录、删除重复记录方法大全
查找所有重复标题的记录:SELECT *FROM t_info aWHERE ((SELECT COUNT(*)FROM t_infoWHERE Title = a.Title) > 1)ORD ...
- SSIS oracle 64位 数据源链接
新建数据源 提供程序选择:本机OLE DB ORACLE Provider For OLE DB 服务器名或文件名中选择Oracle Net Manager中配置好的服务名即可 填写账号,密码,测试, ...