根据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. PHP 数组拼接成字符串

    PHP[知识分享] 数组拼接成字符串 <?php // 格式: [二维数组] Array ( [0] => Array ( [topicid] => 1 ) [1] => Ar ...

  2. ZOJ 1204 一个集合能组成多少个等式

    Additive equations Time Limit : 20000/10000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other ...

  3. window.setTimeout()函数的使用

    <script type="text/javascript"> //此程序主要完成页面定时关闭功能 function closeMyWindow() { window. ...

  4. JS,CSS是前端,JAVA PHP ASP是后端,数据库是后端的处理对象,非代表前后底

    大海-mysql-oracle(529513481)  19:02:18 象我这边,前台都是php,而php做数据分析是不太理想的,做中间件没人力,难办 横瓜(601069289) 19:20:15  ...

  5. properties + IO 读取配置文件

    1.项目目录树 2.配置文件config.properties username = sushe password = sushe url = jdbc:mysql://172.16.100.10:3 ...

  6. jdk 安装配置

    在windows上配置JDK 1.下载windows版JDK 网址:http://www.oracle.com/technetwork/java/javase/archive-139210.html ...

  7. 四、spark常用函数说明学习

    1.parallelize       并行集合,切片数.默认为这个程序所分配到的资源的cpu核的个数.       查看大小:rdd.partitions.size      sc.paraliel ...

  8. My Eclipse Security Alert

    SECURITY ALERT: INTEGRITY CHECK ERROR This product did not pass the MyEclipse integrity check. This ...

  9. install plugin elasticsearch-analysis-ik

    1.github下载分词器插件(请各位下载自己elasticsearch相对应的版本,否则会有兼容性问题) https://github.com/medcl/elasticsearch-analysi ...

  10. python学习——DAY1

    日期:20170113 一.个人体会: 零基础学python,是艰辛的,需要付出和坚持. 关于流程图.我最开始画的是从上到下,再从左到右,画了很多重复的内容,单线程的流程图,看起来很容易理解,但是自己 ...