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(三)组件的生命周期的更多相关文章

  1. reactjs入门到实战(七)---- React的组件的生命周期

    React的组件的生命周期有三个状态分别是:挂载(生产组件示例化.准备挂载到页面.挂载到页面).更新(更新值.更新DOM).和卸载(卸载后). >>>其他     getInitia ...

  2. react native组件的生命周期

    react native组件的生命周期 一.当页面第一次加载时,会依次调用: constructor() componentWillMount(): 这个函数调用时机是在组件创建,并初始化了状态之后, ...

  3. Android React Native组件的生命周期及回调函数

    熟悉android的童鞋应该都清楚,android是有生命周期的,其很多组件也是有生命周期.今天小编和大家分享的React Native组件的生命周期,还不了解的童鞋,赶紧来围观吧 在android开 ...

  4. 【RN - 基础】之React Native组件的生命周期

    下图描述了React Native中组件的生命周期: 从上图中可以看到,React Native组件的生命周期可以分为初始化阶段.存在阶段和销毁阶段. 实例化阶段 实例化阶段是React Native ...

  5. React Native组件、生命周期及属性传值props详解

    创建组件的三种方式 第一种:通过ES6的方式创建 /** * 方式一 :ES6 */ export default class HelloComponent extends Component { r ...

  6. React:组件的生命周期

    在组件的整个生命周期中,随着该组件的props或者state发生改变,其DOM表现也会有相应的变化.一个组件就是一个状态机,对于特定地输入,它总返回一致的输出. 一个React组件的生命周期分为三个部 ...

  7. react教程 — 组件的生命周期 和 执行顺序

    一.组件执行的生命周期:                  参考  https://www.cnblogs.com/soyxiaobi/p/9559117.html  或  https://www.c ...

  8. React Native——组件的生命周期

    组件生命周期 上流程图描述了组件从创建.运行到销毁的整个过程,可以看到如果一个组件在被创建,从开始一直到运行会依次调用getDefaultProps到render这五个函数:在运行过程中,如果有属性和 ...

  9. React入门--------组件的生命周期

    Mounting/组件挂载相关: componentWillMount componentDidMount Updating/组件更新相关: componentWillReceiveProps sho ...

随机推荐

  1. iis最大工作进程数

    IIS 6.0允许将应用程序池配置成一个Web园(Web Garden).要理解Web园的概念,可以设想这样一种情形:假设有一个IIS 5.0服务器和三个Web网站,每一个Web网站运行着相同的应用程 ...

  2. 解决 IE 6/7 中console对象兼容性问题

    话不多说,直接上代码 (function (){ //创建空console对象,避免JS报错 if(!window.console) window.console = {}; var console ...

  3. 如何用70行Java代码实现深度神经网络算法(转)

    对于现在流行的深度学习,保持学习精神是必要的——程序员尤其是架构师永远都要对核心技术和关键算法保持关注和敏感,必要时要动手写一写掌握下来,先不用关心什么时候用到——用不用是政治问题,会不会写是技术问题 ...

  4. iOS navigationcontroller pop 回到上一层视图 如何刷新

    1.从视图A中navigation controller push到视图B,当视图B navigationcontroller pop回到视图A时,并不会调用A的viewDidLoad,但是会调用vi ...

  5. STL学习

    //vector的使用 //相当于数组,常用的 添加 删除 清空 测长 操作 #include<iostream> #include<algorithm> #include&l ...

  6. ps(process status)

    1.PS ps -a(all):显示现行终端机下的所有进程,包括其他用户的进程: ps -ax: 同时加上x参数会显示没有控制终端的进程. ps  -j:显示与作业有关的信息:会话ID.进程组ID等 ...

  7. vmware下的linux的host only上网配置

    1.虚拟机 的网络适配器类型,选择Host-only.启动时修改网络适配器类型需要关电源重启. 2.本机电脑设置,网络邻居 启用 VMware Virtual Ethernet Adapter for ...

  8. 测试RAC的功能

    1.查看RAC服务状态 node1-> crs_stat -t Name           Type           Target    State     Host ---------- ...

  9. JavaFX結合 JDBC, Servlet, Swing, Google Map及動態產生比例圖 (2):JavaFX建立及程式碼說明 (转帖)

    說明:就如同標題一樣,前端會用到JavaFX.Swing.Java Web Start.Google Map 的技術, 後端就是JDBC.Servlet的技術,以及我們會簽署認證jar檔案,這樣才可存 ...

  10. [.net 面向对象编程基础] (19) LINQ基础

    [.net 面向对象编程基础] (19)  LINQ基础 上两节我们介绍了.net的数组.集合和泛型.我们说到,数组是从以前编程语言延伸过来的一种引用类型,采用事先定义长度分配存储区域的方式.而集合是 ...