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 ...
随机推荐
- reporting services rdl文件如何不分页
进入rdl文件中的xml内容,找到<page></page>标签,在该标签中,添加如下属性, <InteractiveHeight>0in</Interact ...
- mentohust 你让我如何说你是好呢?
最近换了ubuntu系统结果热了不少的麻烦, 借此机会唠叨一下, 首先是你这个ubuntu16.4 你这个bug 太让人郁闷了吧,或许主要是应该怪我菜,装个看家的软件eclipse.还热除了一堆的麻 ...
- javascript基础知识-数组
1.javascript创建数组时无需声明数组大小或者在数组大小变化时重新分配 2.javascript数组是无类型的 3.数组元素不一定要连续 4.针对稀疏数组,length比所有元素的索引都要大 ...
- poi解析excel 03、07
maven依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</a ...
- StoryBoard 简单使用
StoryBoard简单使用 故事版(storyboard)是一种简洁的图形界面,程序员可以采取拖的形式搭建一个界面,现在使用的xcode默认都会创建一个main.storyboard,作为app的入 ...
- 地图、定位 CLLocationManager CLGeocoder CLPlacemark
地图.定位 一.基本知识点 定位: 1.info.plist文件设置 ios8以后,使用定位需要在info.plist文件中添加两个字段NSLocationAlwaysUsageDescription ...
- IO-01. 表格输出(5)
本题要求编写程序,按照规定格式输出表格. 输入格式: 本题目没有输入. 输出格式: 要求严格按照给出的格式输出下列表格: ------------------------------------ Pr ...
- alert的换行问题
一种比较复杂的方法,但这种方法使用起来对所有型号的浏览器都能任意分辨: //浏览器类型判定 function getOs() { if(navigator.userAgent.indexOf(&quo ...
- jqurey 中dialog未定义问题
$(function () { $('#btnIndustry').click(function () { $.dialo ...
- git 撤销提交的文件
一.问题 近期在使用git时遇到一个问题,就是在git上传文件时,将一个100兆的大文件添加到了git,并执行了push操作,这时在上传完毕后,会提示这个错误 Large files detected ...