• Mounting/组件挂载相关:

componentWillMount

componentDidMount

  • Updating/组件更新相关:

componentWillReceiveProps

shouldComponentUpdate

componentWillUpdate

componentDidUpdate

  • Unmounting/组件移除相关:

componentWillUnmount

一、Mounting/组件挂载相关

componentWillMount

在组件挂载之前执行,但仅执行一次,即使多次重复渲染改组件,或者改变了组件的state。若希望该回到能执行多次,可以使用ReactDOM.unmountComponentAtNode移除掉已有的组件,然后再重新render。

  1. var diva = document.getElementById('a');
  2. var divb = document.getElementById('b');
  3. var i=;
  4. var Component1 = React.createClass({
  5. componentWillMount:function(){
  6. console.log(++i)
  7. },
  8. render: function() {
  9. console.log(Date.now());
  10. return <div name={this.props.name}>我只是一个安静的div</div>
  11. }
  12. });

  13.   //触发componentWillMount,render
  14. ReactDOM.render(
  15. <Component1 />, diva
  16. );
      
      //未触发componentWillMount,触发render
  17. ReactDOM.render(
  18. <Component1 />, diva
  19. );

  20.   //触发componentWillMount,render
  21. ReactDOM.render(
  22. <Component1 />, divb
  23. );
      //未触发componentWillMount,未触发render
  24. ReactDOM.render(
  25. <Component1 />, divb
  26. );

componentDidMount

在组件挂载之后执行,同componentWillMount一样,同一个组件重复渲染只执行一次,卸载组件后重新渲染可以重新触发一次。

二、Updating/组件更新相关

componentWillReceiveProps

在组件接收到props的时间点之前调用,注意组件初始化渲染时不会调用。

  1. var i = 0;
  2. var div = document.getElementById('a'),
  3. var div2 = document.getElementById('b');
  4.  
  5. var Component1 = React.createClass({
  6. componentWillReceiveProps: function(){
  7. console.log(i++)
  8. },
  9. clickCb: function() {
  10. React.render(
  11. <Component1 />, div2
  12. );
  13. },
  14. render: function() {
  15. return <div onClick={this.clickCb}>点我给下一个div挂载组件</div>
  16. }
  17. });
  18.  
  19. React.render(
  20. <Component1 />, div //初始化不会触发componentWillReceiveProps
  21. );
  22. React.render(
  23. <Component1 />, div //重复渲染会触发componentWillReceiveProps
  24. );
  25. React.unmountComponentAtNode(div); //移除掉已有组件
  26. React.render(
  27. <Component1 />, div //初始化不会触发componentWillReceiveProps
  28. );

运行结果如下:

该接口带有一个参数nextProps,可以利用它来获取新的props的值(this.props获取到的是当前的,也就是旧的props)

  1. var i = 0;
  2. var div = document.getElementById('a');
  3. var render = function(){
  4. React.render(
  5. <Component1 i={i++} />, div
  6. );
  7. };
  8.  
  9. var Component1 = React.createClass({
  10. componentWillReceiveProps: function(nextProps){
  11. console.log(this.props.i, nextProps.i)
  12. },
  13. render: function() {
  14. return <div onClick={render}>props.i的值是:{this.props.i}</div>
  15. }
  16. });
  17. render();

运行结果如下

shouldComponentUpdate

该接口实际是在组件接收到了新的props或者新的state的时候会立即调用,然后通过返回值来决定是否要重新渲染当前的组件。

接口带两个参数,第一个参数表示新的props,第二个参数表示新的state。

  1. var div = document.getElementById('a');
  2.  
  3. var Component1 = React.createClass({
  4. getInitialState: function(){
  5. return { i : }
  6. },
  7. shouldComponentUpdate: function(nextProps, nextState){
  8. console.log( this.state.i, nextState.i );
  9. return nextState.i > ? true : false; //返回true才会渲染组件
  10. },
  11. clickCb: function(){
  12. this.setState({
  13. i : this.state.i +
  14. })
  15. },
  16. render: function() {
  17. return <div onClick={this.clickCb}>state.i的值是:{this.state.i}</div>
  18. }
  19. });
  20. ReactDOM.render(
  21. <Component1 />, div
  22. );

点击第四次之后才会渲染组件,在div里显示正确的新state.i

componentWillUpdate

同shouldComponentUpdate一样,在组件收到新的props或者state的时候会调用,而且也有着两个参数来获取新的props和state。

不过本接口仅在shouldComponentUpdate执行并返回了true的时候才会被调用。

在上一个代码实例中做点改动

  1. componentWillUpdate: function(nextProps, nextState){
  2. console.log( 'yoyoyo', this.state.i, nextState.i );
  3. },

componentDidUpdate

在组件更新,重新渲染完毕之后调用,它和componentWillUpdate一样有着两个参数来获取新的props和state

  1. var div = document.getElementById('a');
  2.  
  3. var Component1 = React.createClass({
  4. getInitialState: function(){
  5. return { i : }
  6. },
  7. shouldComponentUpdate: function(nextProps, nextState){
  8. console.log( this.state.i, nextState.i );
  9. return nextState.i > ? true : false; //返回true才会执行componentWillUpdate并重新渲染组件
  10. },
  11. componentDidUpdate: function(nextProps, nextState){
  12. console.log( '已经渲染完毕咯', this.state.i, nextState.i );
  13. },
  14. componentWillUpdate: function(nextProps, nextState){
  15. console.log( '还没渲染哦', this.state.i, nextState.i );
  16. },
  17. clickCb: function(){
  18. this.setState({
  19. i : this.state.i +
  20. })
  21. },
  22. render: function() {
  23. return <div onClick={this.clickCb}>state.i的值是:{this.state.i}</div>
  24. }
  25. });
  26. ReactDOM.render(
  27. <Component1 />, div
  28. );

运行结果如下:

三、Unmounting/组件移除相关:

  1. var div = document.getElementById('a');
  2. var div2 = document.getElementById('b');
  3.  
  4. var Component1 = React.createClass({
  5.     DOMArr : [],
  6.     getInitialState: function(){
  7.       return { i : }
  8.     },
  9.     componentDidUpdate: function(nextProps, nextState){
  10.       var dom = document.createElement('p');
  11.       dom.innerText = this.state.i;
  12.       div2.appendChild(dom);
  13.       this.DOMArr.push(dom);
  14. },
  15.     componentWillUnmount: function(){
  16.       if(!this.DOMArr.length) return;
  17.       var i = ;
  18.       while(i < this.DOMArr.length){console.log(i);
  19.         div2.removeChild(this.DOMArr[i++]); //移除componentDidUpdate里添加过的DOM
  20.       }
  21.     },
  22.     clickCb: function(){
  23.       this.setState({
  24.         i : this.state.i +
  25.       })
  26.     },
  27.     render: function() {
  28.       return <div onClick={this.clickCb}>state.i的值是:{this.state.i}</div>
  29.     }
  30. });
  31. ReactDOM.render(
  32.   <Component1 />, div
  33. );
  34.  
  35. div2.addEventListener('click',function(){
  36.   ReactDOM.unmountComponentAtNode(div) //点击div2则卸载掉第一个div里的组件
  37. }, false)

运行结果如下:

四、getDefaultProps和getInitialState

getDefaultProps

该方法是所有我们提及的方法中最先触发的,可以在该方法里return 一个对象来作为组件默认的props值(如果父组件传进来了props,以父组件为主),它只在组件初次挂载到页面上时触发,即使你重新挂载了组件。

getInitialState

用于给组件初始化state的值,调用该方法要求必须return 一个对象或者null,否则报错。该方法在组件每次实例化的时候都会触发。

  1. var diva = document.getElementsByTagName('div')[],
  2. divb = document.getElementsByTagName('div')[];
  3. var Component1 = React.createClass({
  4. getDefaultProps: function(){
  5. console.log('getDefaultProps');
  6. return { name : Date.now() }
  7. },
  8. getInitialState: function(){
  9. console.log('getInitialState');
  10. return null; //必须返回一个null或对象,否则会报错
  11. },
  12.  
  13. render: function() {
  14. console.log(Date.now());
  15. return <div name={this.props.name}>我只是一个安静的div</div>
  16. }
  17. });
  18. ReactDOM.render(
  19. {/* 触发一次 getDefaultProps 和 getInitialState */}
  20. <Component1 />, diva
  21. );
  22. ReactDOM.render(
  23. {/* getDefaultProps 和 getInitialState都不触发 */}
  24. <Component1 />, diva
  25. );
  26. ReactDOM.unmountComponentAtNode(diva);
  27. ReactDOM.render(
  28. {/* 触发一次getInitialState */}
  29. <Component1 name="a"/>, diva
  30. );
  31. ReactDOM.render(
  32. {/* 触发一次getInitialState */}
  33. <Component1/>, divb
  34. );

五、总结

上面是9个周期接口,它们被触发的顺序?

  1. var Component1 = React.createClass({
  2. getDefaultProps: function(){
  3. console.log('getDefaultProps')
  4. },
  5. getInitialState: function(){
  6. console.log('getInitialState');
  7. return null
  8. },
  9. componentWillMount: function(){
  10. console.log('componentWillMount')
  11. },
  12. componentDidMount: function(){
  13. console.log('componentDidMount')
  14. },
  15. componentWillReceiveProps: function(){
  16. console.log('componentWillReceiveProps')
  17. },
  18. shouldComponentUpdate: function(){
  19. console.log('shouldComponentUpdate');
  20. return true;
  21. },
  22. componentWillUpdate: function(){
  23. console.log('componentWillUpdate')
  24. },
  25. componentDidUpdate: function(){
  26. console.log('componentDidUpdate')
  27. },
  28. componentWillUnmount: function(){
  29. console.log('componentWillUnmount')
  30. },
  31. render: function() {
  32. return <div>我只是一个安静的div</div>
  33. }
  34. });
  35. ReactDOM.render(
  36. <Component1 />, document.body
  37. );
  38. ReactDOM.render(
  39. <Component1 />, document.body
  40. );
  41. ReactDOM.unmountComponentAtNode(document.body)

结果如下:

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(三)组件的生命周期

    Component Specs and LifeCycle <div id="app"></div> <script src="bower_ ...

随机推荐

  1. Session监听器

    Session监听器,是用来监听session对象创建和关闭的.有时我们需要在session创建或关闭时执行一些操作.这是就可以使用Session Listenner. .在项目的web.xml文件中 ...

  2. .NET应用服务器

    昨天参加Oracle的一个活动,知道了WebLogic的强大,于是对应用服务器产生了兴趣. WebLogic是一个Java EE应用服务器(与Java EE对应的另外一个技术平台就是.NET). 为什 ...

  3. 漫谈可视化Prefuse(二)---一分钟学会Prefuse

    前篇<漫谈可视化Prefuse(一)---从SQL Server数据库读取数据>主要介绍了prefuse如何连接数据库sql server并读取数据进行可视化展现. 回头想想还是应该好好捋 ...

  4. 没有R.java问题找不到getActionBar()方法

    android项目,可是项目中没有重要的R.java,并且报错,说是找不到getActionBar()方法,上网寻找原因,终于寻得解决方法:    1.解决项目中没有R.java问题.在Eclipse ...

  5. MySql操作时间

    今天 select * from 表名 where to_days(时间字段名) = to_days(now()); 昨天 7天 DAY) <= date(时间字段名) 近30天 DAY) &l ...

  6. Azure Redis Cache (3) 在Windows 环境下使用Redis Benchmark

    <Windows Azure Platform 系列文章目录> 熟悉Redis环境的读者都知道,我们可以在Linux环境里,使用Redis Benchmark,测试Redis的性能. ht ...

  7. Maven提高篇系列之(二)——配置Plugin到某个Phase(以Selenium集成测试为例)

    这是一个Maven提高篇的系列,包含有以下文章: Maven提高篇系列之(一)——多模块 vs 继承 Maven提高篇系列之(二)——配置Plugin到某个Phase(以Selenium集成测试为例) ...

  8. JAVA - 多线程 两种方法的比较

    一.继承Thread类 实现方法: (1).首先定义一个类去继承Thread父类,重写父类中的run()方法.在run()方法中加入具体的任务代码或处理逻辑.(2).直接创建一个ThreadDemo2 ...

  9. [水煮 ASP.NET Web API2 方法论](3-9)空气路由的设置

    阅读导航 问题 解决方案 工作原理 代码演示 在此解释一下,空气路由,是本人臆想出来,觉着更能表达 IgnoreRoute 的意图,如果看着辣眼睛^^,请见谅. 问题 我们在之定义过集中式路由,集中式 ...

  10. 【SQL】CLR聚合函数什么鬼

    之前写过一个合并字符串的CLR聚合函数,基本是照抄MS的示例,外加了一些处理,已经投入使用很长时间,没什么问题也就没怎么研究,近日想改造一下,遇到一些问题,遂捣鼓一番,有些心得,记录如下. 一.杂项 ...