根据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的更多相关文章

  1. React学习笔记--程序调试

    React学习笔记 二 程序调试   前面我们搭建好了React的基本开发环境,可以编写基本的React js程序了.但完成的开发环境肯定包含调试器,怎么调试用React编写的JS程序呢?有浏览器,比 ...

  2. react学习笔记1--基础知识

    什么是react A JAVASCRIPT LIBRARY FOR BUILDING USER INTERFACES[React是一个用于构建用户界面的JavaScript库.] React之所以快, ...

  3. React学习笔记(一)- 入门笔记

    React入门指南 作者:狐狸家的鱼 本文链接:React学习笔记 GitHub:sueRimn 1.组件内部状态state的修改 修改组件的每个状态,组件的render()方法都会再次运行.这样就可 ...

  4. 机器学习实战(Machine Learning in Action)学习笔记————05.Logistic回归

    机器学习实战(Machine Learning in Action)学习笔记————05.Logistic回归 关键字:Logistic回归.python.源码解析.测试作者:米仓山下时间:2018- ...

  5. React学习笔记(七)条件渲染

    React学习笔记(七) 六.条件渲染 使用if或条件运算符来创建表示当前状态的元素. 可以使用变量来存储元素.比如: let button = null; if (isLoggedIn) { but ...

  6. React学习笔记(六)事件处理

    React学习笔记(六) 五.事件处理 React事件绑定属性的命名采用驼峰写法,不同于传统DOM全部小写. 如果采用JSX的语法,事件函数需要用大括号{}包裹函数名,不同于传统DOM字符串小括号的方 ...

  7. React学习笔记(五)State&声明周期

    React学习笔记(五) 四.State&声明周期 可以为组件添加"状态(state)".状态与属性相似,但是状态是私有的,完全受控于当前组件. 局部状态就是只能用于类(定 ...

  8. React学习笔记 - 组件&Props

    React Learn Note 4 React学习笔记(四) 标签(空格分隔): React JavaScript 三.组件&Props 组件可以将UI切分成一些独立的.可复用的部件,这样你 ...

  9. React学习笔记 - 元素渲染

    React Learn Note 3 React学习笔记(三) 标签(空格分隔): React JavaScript 二.元素渲染 元素是构成react应用的最小单位. 元素是普通的对象. 元素是构成 ...

随机推荐

  1. 工具条OutLookBar

    工具条OutLookBar 灰姑娘本身也有自已的优点,但是却可能因为外貌不讨人喜欢,要变成白雪公主却需要有很多勇气和决心去改变自已: 有一颗善良的心 讨人喜爱的外貌 我这里讲的是一个工具条的蜕变过程, ...

  2. 发布Activex全过程

    C#制作.打包.签名.发布Activex全过程 一.前言 最近有这样一个需求,需要在网页上面启动客户端的软件,软件之间的通信.调用,单单依靠HTML是无法实现了,因此必须借用Activex来实现.由于 ...

  3. 超级强悍的PHP代码编辑器PHPstorm及配置

    如何下载安装 官方网站:http://www.jetbrains.com/phpstorm/,本篇文章展示的是5.0版本以,所以,如果你的软件版本过高,可能可有误,所以,如果有问题,请在本站留言,做为 ...

  4. JavaScript中的call 和apply的用途以及区别

    apply 接受两个参数,第一个参数指定了函数体内this 对象的指向,第二个参数为一个带下标的集合,这个集合可以为数组,也可以为类数组,apply 方法把这个集合中的元素作为参数传递给被调用的函数: ...

  5. Hybrid App(一)App开发选型

    1.几种app开发模式概述 Native App 即传统的原生APP开发模式,Android基于Java语言,底层调用Google的 API;iOS基于OC或者Swift语言,底层调用App官方提供的 ...

  6. FTP之虚拟用户

    基于虚拟用户访问ftp关闭防火墙,selinux 过程如下1.装包,配置.起服务配置过程如下: 需写入vsftpd.conf配置文件中的内容如下: anonymous_enable=NO ---- 匿 ...

  7. html5 canvas的教程

    原文地址:http://www.cnblogs.com/tim-li/archive/2012/08/06/2580252.html 原作很强悍 导航 前言 基本知识 绘制矩形 清除矩形区域 圆弧 路 ...

  8. hdu1041

    #include <iostream> #include <string> using namespace std; const int SIZE = 1001; const ...

  9. jmeter连接数据库

    新建一个 Thread Group: 新增 JDBC Connection Configuration: 点击新增的 JDBC Connection Configuration ,需要修改的参数包括: ...

  10. iOS 10 之后,相机权限问题及易出现的Crash

    1: iOS 10 之后,访问相机需要设置相关的权限 麦克风权限:Privacy - Microphone Usage Description 是否允许此App使用你的麦克风? 相机权限: Priva ...