react生命周期分享,不管你是否看过!!!

1、基础

先来介绍一个生命周期的定义:
1)componentWillMount(){}react

// Mounting  安装阶段 
// 在客户端和服务器上,在初始渲染发生以前当即调用一次 若是在这个方法中调用setState,
// render()将看到更新的状态,而且只会执行一次,尽管状态改变。

2)componentDidMount(){}es6

// Mounting  安装阶段 
// 调用一次,只在客户端(不在服务器上),在初始渲染发生后当即
// 子组件的componentDidMount()方法在父组件的componentDidMount()方法以前被调用
// setTimeout  setInterval   AJAX 在此之行,强烈建议

3)componentWillReceiveProps(nextProps){}服务器

// Updating 更新阶段
// 在组件接收新props时调用。初始渲染不调用此方法。
// 老的props能够用this.props  新值就用nextProps查看
// 在此函数中调用this.setState()不会触发附加的渲染。

4)shouldComponentUpdate(nextProps, nextState){}函数

// Updating 更新阶段
// 当正在接收新的道具或状态时,在渲染以前调用。
// 此方法必须返回false or true 不然报错  true则渲染,false则不渲染!在此声明周期中能够考虑是否须要进行渲染!避免没必要要的性能浪费
// 若是shouldComponentUpdate返回false,那么render()将被彻底跳过,直到下一个状态改变。此外,不会调用componentWillUpdate和componentDidUpdate。
// 默认返回true
// 若是性能是一个瓶颈,特别是有几十个或几百个组件,请使用shouldComponentUpdate来加快您的应用程序。
// return true or false

5) componentWillUpdate(nextProps, nextState){}性能

// Updating 更新阶段
// 当正在接收新的props或state时当即调用。初始渲染不调用此方法
// 您不能在此方法中使用this.setState()。若是您须要更新state以响应prop更改,请改用componentWillReceiveProps。

6)componentDidUpdate(nextProps, nextState){}ui

// Updating  更新阶段
// 在组件的更新刷新到DOM后当即调用。初始渲染不调用此方法。
// 当组件已更新时,使用此操做做为DOM操做的机会

7)componentWillUnmount(){}this

// Unmounting  卸载阶段
// 在组件从DOM卸载以前当即调用。
// 在此方法中执行任何须要的清理,例如使计时器无效或清除在componentDidMount中建立的任何DOM元素。clearInterval or destroy

2、生命周期的执行顺序

举例:只有一个组件,里面有一个onClick事件改编一个state调试

刷新页面:code

a、componentWillMount--->    // 能够更改state
            render()---->
                componentDidMount // 周期结束

触发onClick事件:(前提只有事件中出发setState,其余中没有)component

shouldComponentUpdate中 return true
    shouldComponentUpdate-->
            componentWillUpdate-->
                    render()-->
                            componentDidUpdate //周期结束
    
shouldComponentUpdate中 return false
            shouldComponentUpdate //周期结束

上面只是一个很简单的例子讲述了周期的执行顺序,你们能够根据顺序去作相应的操做,固然在componentWillUpdatecomponentDidUpdate这两个周期中不可使用this.setState,须要使用此方法能够在componentWillReceiveProps中去操做。周期中可能进行的操做在上面的定义中以解释。

举例:父、子组件,父组件和上述相同,字段件只是一个纯ui组件没有任何的操做,接受父组件传来的props(父组件的click可控制props的内容)。

刷新页面:

父componentWillMount--->父render()---->子componentWillMount--->子render()--->子componentDidMount--->父componentDidMount

触发onClick事件:

子组件shouldComponentUpdate 返回的是false
    父shouldComponentUpdate-->父componentWillUpdate-->父render()-->父componentDidUpdate
子组件shouldComponentUpdate 返回的是true
    父shouldComponentUpdate-->父componentWillUpdate-->父render()--->子componentWillReceiveProps--->子shouldComponentUpdate--->子componentWillUpdate---->子render()--->子componentDidUpdate--->父componentDidUpdate

componentWillUnmount阶段
当你的组件关闭的时候,例如跳转页面的时候,此周期执行一次。可能作的操做在上面的定义。

给出一段代码:(就是上述的子组件)

import React, { Component } from 'react';

class Another extends Component {
    constructor(props) {
        super(props);
        this.state = {
            son:'子组件'
        }
    }
    componentWillMount() {
    console.log('子组件--Mounting-componentWillMount',this.state.son)
  }
  componentDidMount() {
    console.log('子组件--Mounting-componentDidMount',this.state.son)
  }
  componentWillReceiveProps(nextProps) {
    console.log('子组件--Updating-componentWillReceiveProps')
  }
  shouldComponentUpdate(nextProps, nextState) {
    console.log('子组件--Updating-shouldComponentUpdate')
    return true
  }
  componentWillUpdate(nextProps, nextState) {
    console.log('子组件--Updating-componentWillUpdate')
  }
  componentDidUpdate(nextProps, nextState) {
    console.log('子组件--Updating-componentDidUpdate')
  }
  componentWillUnmount() {
  }
    render() {
        return(
            <div>
                我是子组件--我是子组件--我是子组件{this.props.name}
            </div>
        )
    }
}


export default Another;

根据上面的代码,说一个组件实例的初始化的方法过程

1)`getInitialState` 设置初始状态值,(掉调用一次),可以使用setState方法更改状态

es6语法则在这是用:

constructor(props) {
        super(props);
        this.displayName='name';
        this.事件名=this.事件名.bind(this);//绑定事件的this,这样初始化只绑定一次,若是在render中邦定,则只要render就邦定一次。
        this.state = {
            son:'子组件'
        }
    }
    static propTypes = { 
        value:PropTypes.string,
        //类型的种类object string array func number bool any
    }
    static defaultProps={ 
        value:'1'
    }
2)`getDefaultProps `设置初始props的值,不能够更改
es6语法参照上面的 static defaultProps

3)`propTypes `验证传递给组件的props
es6语法上述 static propTypes   

4)`displayName `用于degbug调试消息,若是不写,JSX将从变量赋值中推断出类的displayName,es6语法如上述代码片断中,例以下面
// Input (JSX):
var Nav = React.createClass({ });
// Output (JS):
var Nav = React.createClass({displayName: "Nav", });
执行的顺序就是上述代码片断的顺序!

3、总结

详细了解生命周期的特性,有助于处理数据,更好的节约性能。本人一点点小的看法,还望各位路过的大神,赏脸批评指正!
相关文章
相关标签/搜索