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 ...
随机推荐
- 黑马程序员_Java基础:网络编程总结
------- android培训.java培训.期待与您交流! ---------- Java语言是在网络环境下诞生的,它是第一个完全融入网络的语言,虽然不能说它是对支持网络编程做得最好的语言,但是 ...
- SqlIO优化
1SqlIO优化 set statistics io on--sqlset statistics io off 2Sql占用CPU时间 select c.total_worker_time, c.la ...
- xUtils 中的BitmapUtils 全面注释
/** * 加载图片工具类 * @author afu * */ public class BitmapUtils implements TaskHandler { /** * 判断任务是否暂停 */ ...
- berkeley db replica机制 - 消息处理
repmgr_method.c, __repmgr_start_int()repmgr_method.c, __repmgr_start_msg_threads()repmgr_msg.c, __re ...
- monodevelop 突然莫名其妙的将 warning 全部标记为 error
这是一个关于 monodevelop 的“坑” 我们在用 monodevelop 编译游戏脚本时, 通常会有一些警告,一般这些警告都是无害的, 不影响游戏运行.可是突然有一天, monodevelop ...
- Core Audio(二)
用户模式音频组件 在windows vista中,core audio apis充当用户模式音频子系统的基础,core audio apis作为用户模式系统组件的一个thin layer,它用来将用户 ...
- cowboy学习笔记(安装与部署)
安装cowboy,参照官方文档:http://ninenines.eu/docs/en/cowboy/1.0/guide/getting_started/ 添加依赖库:在makefile中添加,会自动 ...
- 【OC简介-类和对象】
OC与C语言相比,体现的是一种面向对象的思想.OC完全兼容C语言 OC中大多数类前面都有前缀NS,来源于乔布斯NextStep团队,被苹果收购后保留了所有基本语法,合并到Foundation中 OC语 ...
- 浅析C语言指针问题
首先明白c语言操作符的优先级及结合性就很容易理解了. 链接 1.关于char *s 及 char s[] char *s指向的是一个字符串对象的指针,可以理解为间接引用,比如 char *s = “1 ...
- Tesseract-OCR识别中文与训练字库实例
关于中文的识别,效果比较好而且开源的应该就是Tesseract-OCR了,所以自己亲身试用一下,分享到博客让有同样兴趣的人少走弯路. 文中所用到的身份证图片资源是百度找的,如有侵权可联系我删除. 一. ...