美文网首页Web前端之路让前端飞
React入门系列(四)组件的生命周期

React入门系列(四)组件的生命周期

作者: 娜姐聊前端 | 来源:发表于2017-04-26 19:16 被阅读109次

React的核心是组件,组件在创建和渲染的过程中,需要调用固定的钩子函数,也称为组件的“生命周期”。利用生命周期函数,可以做初始化工作,并在渲染过程中实现一些特定功能。

1. 生命周期函数

组件的整个生命周期会涉及如下函数:

钩子函数 说明
getDefaultProps 设置props默认配置
getInitialState 设置state默认配置
componentWillMount 组件被注入DOM之前被调用
render 渲染组件时被调用
componentDidMount 组件被注入DOM之后被调用
componentWillReceiveProps 挂载的组件接收到新的props时被调用
shouldComponentUpdate 指定是否更新props和state
componentWillUpdate 更新组件时,渲染之前被调用
componentDidUpdate 更新组件时,渲染之后被调用
componentWillUnMount 卸载组件

可以参考下图(来自网络)进一步了解整个流程。

react life cycle.jpg

这里特殊说明两个方法:getDefaultPropsgetInitialState

  • React.createClass()函数创建组件,调用的是这两个钩子函数。
  • ES6类方法创建的组件,初始化props用的是静态属性defaultProps;初始化state是在构造函数constructor里做的。

总结:

  • props更改时,会依次调用componentWillReceiveProps -> shouldComponentUpdate -> componentWillUpdate -> render -> componentDidUpdate
  • state更改时,会依次调用shouldComponentUpdate -> componentWillUpdate -> render -> componentDidUpdate
小贴士

shouldComponentUpdate 是一个非常重要的钩子函数,这个函数默认返回true。

在React中,调用setState方法,React不会立即对其更新,而是将其标记为“脏”状态
(组件状态更新不会立刻生效,React使用事件轮询对变更内容进行批量绘制)。
当事件轮询结束后,React将“脏”组件及其子节点进行重绘,所有后代节点的render方法都会被调用,哪怕它们没法发生变化。

通过shouldComponentUpdate方法,可以阻止子树的重绘
(自行实现该方法并返回false,React会跳过该组件及其子组件的重绘过程)。

--- 参考《Pro React》

下面,我们来看一个真实例子,观察组件生命周期的变换(采用ES6类模式)。

2. 组件实例

class DangerButton extends React.Component {
    /*类型检查*/
    static propTypes = {
        onClick: React.PropTypes.func,
        text: React.PropTypes.string
    };

    /*初始化props值*/
    static defaultProps = {
        type: 'btn'
    };

    constructor(props) {
        super(props);
        // 初始化state值
        this.state = {count: 0};
        this.increaseCount = this.increaseCount.bind(this);
    }

    increaseCount() {
        this.setState({count: this.state.count + 1});
    }

    getObjectValues(obj){
        var array = [];
        for(let key in obj){
            array.push(key + ":" + obj[key]);
        }
        return array.join(";");
    }

    /*----------------start: life cycle---------------*/
    /*组件被注入DOM之前*/
    componentWillMount() {
        var button = document.getElementById('dangerBtn');
        console.log("componentWillMount:" + button);
    }
    /*组件被注入DOM之后*/
    componentDidMount() {
        var button = document.getElementById('dangerBtn');
        console.log("componentDidMount:" + button);
    }

    /*挂载的组件接收到新的props时被调用*/
    componentWillReceiveProps(nextProps){
        console.log("componentWillReceiveProps:" + nextProps);
    }

    /*指定是否更新props和state*/
    shouldComponentUpdate(nextProps, nextState){
        console.log("shouldComponentUpdate-true!");
        return true;
    }

    /*更新组件时,渲染之前*/
    componentWillUpdate(nextProps, nextState){
        console.log("componentWillUpdate-nextProps:" + this.getObjectValues(nextProps));
        console.log("componentWillUpdate-nextState:" + this.getObjectValues(nextState));
    }

    /*更新组件时,渲染之后*/
    componentDidUpdate(prevProps, prevState){
        console.log("componentDidUpdate-prevProps:" + this.getObjectValues(prevProps));
        console.log("componentDidUpdate-prevState:" + this.getObjectValues(prevState));
    }

    /*卸载组件*/
    componentWillUnMount() {
        var button = document.getElementById('dangerBtn');
        console.log("componentDidMount:" + button);
    }

    render() {
        console.log("rendering....");
        return (<div>
            <button id="dangerBtn" className='red' onClick={this.increaseCount}>
                <span className='white'>{this.props.text}</span>
            </button>
            <p>Click count: {this.state.count}</p>
        </div>);
    }
    /*----------------end: life cycle---------------*/
}
ReactDOM.render(
    <DangerButton text="click it!"/>,
    document.getElementById('container')
);

第一次渲染DangerButton组件时,控制台打印如下信息:

componentWillMount:null
rendering....
componentDidMount:[object HTMLButtonElement]

可见,渲染组件的componentWillMount阶段,真实DOM还没有生成;到了componentDidMount阶段,组件才真正被加载到DOM中。

然后,点击DangerButton,count值加一,控制台打印如下信息:

shouldComponentUpdate-true!
componentWillUpdate-nextProps:text:click it!;type:btn
componentWillUpdate-nextState:count:1
rendering....
componentDidUpdate-prevProps:text:click it!;type:btn
componentDidUpdate-prevState:count:0

可见,如果组件自身的state更新后(点击button,触发onClick事件),会依次执行shouldComponentUpdate,componentWillUpdaterendercomponentDidUpdate函数。

小结

在组件整个生命周期中,涉及到两种变量来传递/存储值,propstate。那么,它们的使用场景是什么?有什么区别呢?下一节,我们将继续探索......

下一节:React入门系列(五)props和state

微信公众号:

相关文章

  • React概念图

    React概念图 React组件生命周期概念图 参考文档:React入门教程 组件生命周期React:组件生命周期...

  • React Native组件(二)View组件解析

    相关文章React Native探索系列React Native组件系列 前言 了解了RN的组件的生命周期后,我们...

  • Notes On React - Two

    React 的生命周期   React组件 的生命周期大致可分成四个状态:  - Mounting:装配-组件实例...

  • react 生命周期

    React 生命周期文档 1、理解 组件对象从创建到死亡它会经历特定的生命周期阶段 React组件对象包含一系列的...

  • React入门系列(四)组件的生命周期

    React的核心是组件,组件在创建和渲染的过程中,需要调用固定的钩子函数,也称为组件的“生命周期”。利用生命周期函...

  • React 组件生命周期

    组件生命周期 参考阅读: component-lifecycle react组件生命周期过程说明 react 组件...

  • React总结

    [toc] 1.React组件生命周期 1.1 生命周期图 组件的生命周期的图如下: 具体可参考React 组件生...

  • React-Native资料整理

    (1)ES5,ES6对照 (2)react-native入门指南 (3)组件的生命周期 (4)react-nati...

  • 学习并实现react (4)

    实现生命周期 生命周期介绍 React 生命周期图 React 子组件在父组件下的生命周期流程 实现 compon...

  • react(最近搞了一套react项目的视频 全套 有需

    React 组件生命周期在本章节中我们将讨论 React 组件的生命周期。 组件的生命周期可分成三个状态: Mou...

网友评论

    本文标题:React入门系列(四)组件的生命周期

    本文链接:https://www.haomeiwen.com/subject/wbvszttx.html