首页 > web前端 > js教程 > 正文

浅析React组件的生命周期(代码解析)

不言
发布: 2018-09-10 17:34:28
原创
1271 人浏览过

本篇文章给大家带来的内容是关于浅析React组件的生命周期(代码解析),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

整个 React 生命周期有3个阶段:创建、更新、卸载,每个阶段有对应的工作和方法,我们可以看下面这个经典的图研究一下:

3504961011-5b95526cf0b37_articlex.png

第一阶段

这是虚拟 DOM 创建的阶段,会依次执行 5 个方法,这 5 个方法中除了 render 方法,其余四个方法在整个生命周期中只调用 1 次,而且一定会调用 1 次:

  • getDefaultProps()

这个方法在组件实例创建前,也就是构造函数执行前执行,获取父组件传来的参数,你可以在这里编辑参数并返回新的参数作为 props

  • getInitalState()

组件创建的一开始会调用这个方法初始化组件的 state

  • componentWillMount()

在组件 render 之前执行该方法,可用来修改 state。React 先调用父组件的这个函数,再调用子组件的这个函数

  • render()

开始组件渲染函数,返回一个只有一个根节点的虚拟 DOM。该函数中不能同步的修改组件的状态(state)。

  • componentDidMount()

在 render 渲染之后,通知组件已经加载完成。React 先调用子组件的这个函数,再调用父组件的这个函数。从这个函数开始,该组件就可以和其他框架交互了。比如设置计时器或者发起网络请求。

第二阶段

此时该组件已经进入了稳定运行阶段,这个阶段组件可以处理用户交互,或者接收事件更新界面。以下方法在整个生命周期中可以执行很多次,也可以一次也不执行。

  • componentWillReceiveProps()

当父容器中对应的参数改变将会调用子组件的该函数。新的 props 将会作为参数传递进来,老的 props 可以根据 this.props 来获取。我们可以在该函数中对state作一些处理。并且在该函数中更新 state 不会发生二次渲染

  • shouldComponentUpdate()

该函数传递过来两个参数,新的 state 和新的 props。state 和 props 的改变都会调到该函数。该函数主要对传递过来的 nextProps 和 nextState 作判断。如果返回 true 则重新渲染(默认都是返回 true),如果返回 false 则不重新渲染。在某些特定条件下,我们可以根据传递过来的 props 和 state 来选择更新或者不更新,从而提高效率。

  • componentWillUpdate()

与 componentWillMount 方法类似,在 render 渲染之前被调用。组件上会接收到新的 props 或者 state。这个函数调用之后,就会把 nextProps 和 nextState 分别设置到 this.props 和 this.state 中。

  • componentDidUpdate()

与 componentDidMount 方法类似,在 render 渲染之后被调用,真实 DOM 生成之后调用该函数。传递过来的参数是之前的 props 和 state。

第三阶段

这就是消亡的阶段,主要进行内存的清理和释放的工作。这个阶段只有一个方法,该方法在整个生命周期内调用且仅调用一次。

  • componentWillUnmount()

当组件要被从界面上移除的时候,就会调用 componentWillUnmount。在这里进行一些相关的销毁操作,比如撤销定时器,事件监听等等。

触发 render 的几种情况

这里我们仅考虑 shouldComponentUpdate 没有被修改,始终返回的是 true

  • 首次渲染,即 Initial Render

  • 调用this.setState (不是每次调用 setState 都会触发,react 会优化,比如 antd 的 input 组件)

  • 父组件发生更新,通常是修改的子组件的 props

  • 如果父组件触发了 render, 子组件当然也会相应触发 render

  • 调用 this.forceUpdate()

一个简单的示例

import React from 'react';
import ReactDOM from 'react-dom';
import style from './font.css';
import './index.less';

class Parent extends React.Component{
  constructor(props) {
    super(props);
    this.state = {
      willRender: true,
      prop: 1
    };
  }

  render(){
    return (
      <div>
        <button onClick={()=>{this.setState({prop: 10})}}>changePropsFromParent</button>
        {
          this.state.willRender &&
          <Child fromParent={this.state.prop}/>
        }
        <button onClick={()=>{this.setState({willRender: false})}}>UnmountChild</button>
      </div>
    );
  }
}

class Child extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      curr: 0
    };
  }

 getDefaultProps(){
   console.log('getDefaultProps');
 }

  getInitalState(){
    console.log('getInitalState');
  }

  componentWillMount(){
    console.log('componentWillMount');
  }

  componentDidMount(){
    console.log('componentDidMount');
  }

  componentWillReceiveProps(){
    console.log('componentWillReceiveProps');
  }

  shouldComponentUpdate(){
    console.log('shouldComponentUpdate');
    return true;
  }

  componentWillUpdate(){
    console.log('componentWillUpdate');
  }

  componentDidUpdate(){
    console.log('componentDidUpdate');
  }

  componentWillUnmount(){
    console.log('componentWillUnmount');
  }

  render() {
    console.log('render')

    return (
      <div>
        <button onClick={()=>this.setState({curr:2})}>setState</button>
        <button onClick={()=>{this.forceUpdate();}}>forceUpdate</button>
      </div>
    );
  }
}

ReactDOM.render(
  <Parent />,
  document.getElementById('root')
);
登录后复制

相关推荐:

React组件生命周期实例分析

React Native组件的生命周期多长

以上是浅析React组件的生命周期(代码解析)的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!